Solved

Capture kill command execution

Posted on 2011-02-21
10
587 Views
Last Modified: 2012-05-11
Hello,

In a Bash script, I have to set a condition as shown. The script should execute kill command (with signal 15) to kill a Process ID. Based on the successful killing of process ID, the script should report the user. What I observed is, when I issue an kill command, regardless of process ID gets killed or not, am getting the EXIT status (captured with variable RETVAL) as ZERO. Please let me know how can I capture the status of PID gets killed or not.

stop ()
{
        kill -15 "$JBOSSPID"
        RETVAL=$?
        if [ "$RETVAL" == "0" ]; then
        echo "Admin Application has been stopped" && echo
        rm -rf /var/lock/subsys/admin
        echo "$CURRENT_TIME: Admin application has been stopped." >> $LOGFILE
        else
        echo "Couldn't stop Jboss process. Please check it manually." && echo
        fi

}
0
Comment
Question by:ashsysad
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 40

Expert Comment

by:omarfarid
ID: 34945954
try to change

if [ "$RETVAL" == "0" ]; then

to

if [ $RETVAL -eq 0 ] ; then
0
 

Author Comment

by:ashsysad
ID: 34946074
I'm afriad you didn't get my question properly. I don't have any issue with syntax in script.

Please note a 0 return value on a kill just means that kill() has succeeded in sending the signal to the process, not that the process has responded and shut down.
http://www.mkssoftware.com/docs/man3/kill.3.asp.

So after executing the kill command, the script should check if the PID got killed properly and based on that it should go to further steps. So the question is, how to check the successful completion of a kill command (with -15 signal).
0
 
LVL 40

Assisted Solution

by:omarfarid
omarfarid earned 50 total points
ID: 34946137
you may use ps with grep to see if the process is there or not

0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

Author Comment

by:ashsysad
ID: 34946165
Yes, greping from 'ps' output is an option. Wondering if there is any straight-forward method or command ?
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 50 total points
ID: 34946377
"kill", when issued against an existing PID will always return zero.

grep and ps are your only options.

wmp
0
 
LVL 34

Expert Comment

by:Duncan Roe
ID: 34949571
You could try a short sleep then kill -0 (signal zero). If return is zero, then pid is still there, otherwise not.
0
 
LVL 34

Expert Comment

by:Duncan Roe
ID: 34949580
or kill -0 2>/dev/null to avoid unwanted output
0
 
LVL 34

Expert Comment

by:Duncan Roe
ID: 34949590
(If the process has a signal handler you need to give the handler some time to do its thing)
0
 
LVL 3

Accepted Solution

by:
ivailoj earned 300 total points
ID: 34950158
handy:
cat /proc/PID/status

kill - just send signal and not wait for result.

please read for:
wait
or
trap

I suppose you just must wait for the process real exit to unlock files and sockets if this is the case use:
lsof

0
 

Author Closing Comment

by:ashsysad
ID: 35014821
I like the solution given by ivailoj and it did helped me to resolve the issue.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question