I have a start|stop|restart script for a custom app we have. After it tries to stop our process the correct way, it checks to see if it's gone, if not it tries to kill it, if that doesn't work kill -9.
If I run kill -9 on the PID from the command line it kills it and all is well. If I have the same command within my script it kills it properly, but it also kicks out some usage info. I know I could redirect stdout/stderr for the kill command within the script but I'd rather not if possible.
Here's the code snip:
# Check running processes against our list of targets. If any of
# them are still running try to kill.
stoptargetcount=${#stoptargets[@]}
for ((i=0;i<$stoptargetcount;i++)); do
found=0
for ((j=0;j<$resincount;j++)); do
if [ "${stoptargets[$i]}" == "${instancename[$j]}" ]; then
found=1
killjavapid=${javapid[$j]}
killwrapperpid=`ps alx | grep $killjavapid | grep -v "grep" | awk '{ print $4 }'`
fi
done
if [ $found -eq 1 ]; then
if [ $pass -eq 1 ]; then
echo "${stoptargets[$i]} appears to be hung. Attempting kill..."
sudo kill $killwrapperpid
sudo kill $killjavapid
waiting 5
elif [ $pass -eq 2 ]; then
echo "${stoptargets[$i]} is still hung. Attempting kill -9..."
echo "WARNING - This may result in zombied child processes"
sudo kill -9 $killwrapperpid
sudo kill -9 $killpid
waiting 5
else
echo "ERROR - There appears to be something wrong with the script...exiting"
exit 1
fi
recheck=1
fi
done
and the output...
Checking instance selection...
inst04 found
inst02 found
Stopping instances...
Waiting 5 seconds...
inst04 appears to be hung. Attempting kill...
Waiting 5 seconds...
inst02 appears to be hung. Attempting kill...
Waiting 5 seconds...
inst04 is still hung. Attempting kill -9...
WARNING - This may result in zombied child processes
usage: kill [ -s signal | -p ] [ -a ] pid ...
kill -l [ signal ]
Waiting 5 seconds...
inst02 is still hung. Attempting kill -9...
WARNING - This may result in zombied child processes
usage: kill [ -s signal | -p ] [ -a ] pid ...
kill -l [ signal ]
Waiting 5 seconds...