We use pkill to check if a program is running to kill it before restarting it when new code is being deployed.
pkill --signal 15 --pidfile programname.pid
This normally works, but when there is a problem and the application program hangs, signal 15 doesn't kill the process. The exit codes for pkill are:
EXIT STATUS
0 One or more processes matched the criteria.
1 No processes matched.
2 Syntax error in the command line.
3 Fatal error: out of memory etc.
For obvious reasons to me after thinking about it, it cannot return a code to indicate that the process kill was successful, since pkill only sends a signal to it and nothing more. So I will have to check if the process was ended and if not try again with signal 9 and if not killed yet, try with signal 6 (or some others even).
So I guess doing some kind of if/elif/else/fi to test for this should suffice:
#!/bin/bash
pkill --signal 15 --pidfile programname.pid
result1=$?
echo 'result1 is ' $result1
if [ "$result1" -eq 0 ]
then
echo "programname found running"
pgrep --pidfile programname.pid
result2=$?
if [ "$result2" -eq 1 ]
then
# happens if pid# is not found
echo "programname killed successfully with signal 15"
# restart the program that was killed
else
pkill --signal 9 --pidfile programname.pid
result3=$?
if [ "result3" -eq 0 ]
then
pgrep --pidfile programname.pid
result4=$?
if [ "result4" -eq 1 ]
then
echo "programname killed successfully with signal 9"
# restart the pgram that was killed
fi
fi
fi
elif [ $result1 -eq 1 ]
then
echo "programname pid# not found. Is the system running?"
else
echo "pkill exited with error" $result1
fi
echo 'Continue...'
This all seems a bit convoluted, or am I just being pedantic? Is there a better way to achieve this result in a script?