?
Solved

Capture kill command execution

Posted on 2011-02-21
10
Medium Priority
?
595 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 150 total points
ID: 34946137
you may use ps with grep to see if the process is there or not

0
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

 

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 150 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 35

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 35

Expert Comment

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

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 900 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
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 how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses
Course of the Month17 days, 1 hour left to enroll

864 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