Capture kill command execution

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

}
ashsysadAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

omarfaridCommented:
try to change

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

to

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.
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).
omarfaridCommented:
you may use ps with grep to see if the process is there or not

IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

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

grep and ps are your only options.

wmp
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)
ivailojCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ashsysadAuthor Commented:
I like the solution given by ivailoj and it did helped me to resolve the issue.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.