Solved

Capture kill command execution

Posted on 2011-02-21
10
585 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
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…
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.

912 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

15 Experts available now in Live!

Get 1:1 Help Now