Solved

Capture kill command execution

Posted on 2011-02-21
10
584 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
 

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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 …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.:

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now