Capture kill command execution


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"
        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
        echo "Couldn't stop Jboss process. Please check it manually." && echo

Who is Participating?
ivailojConnect With a Mentor Commented:
cat /proc/PID/status

kill - just send signal and not wait for result.

please read for:

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

try to change

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


if [ $RETVAL -eq 0 ] ; then
ashsysadAuthor Commented:
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.

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).
Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

omarfaridConnect With a Mentor Commented:
you may use ps with grep to see if the process is there or not

ashsysadAuthor Commented:
Yes, greping from 'ps' output is an option. Wondering if there is any straight-forward method or command ?
woolmilkporcConnect With a Mentor Commented:
"kill", when issued against an existing PID will always return zero.

grep and ps are your only options.

Duncan RoeSoftware DeveloperCommented:
You could try a short sleep then kill -0 (signal zero). If return is zero, then pid is still there, otherwise not.
Duncan RoeSoftware DeveloperCommented:
or kill -0 2>/dev/null to avoid unwanted output
Duncan RoeSoftware DeveloperCommented:
(If the process has a signal handler you need to give the handler some time to do its thing)
ashsysadAuthor Commented:
I like the solution given by ivailoj and it did helped me to resolve the issue.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.