I'm writing a simple bash script to shutdown tomcat, and if it doesn't stop gracefully then check if the tomcat's PID still exists and kill it.
I pass the tomcat name as a variable to the script as below. In some instances I pass two or three names of tomcat, which is why the use of FOR LOOP below
./shutdown.sh tomcat1
Content of the Shutdown.sh script
#!/bin/bash
for name in "$@"
do
bash /opt/$name/bin/shutdown.sh
done
sleep 30
for name in "$@"
do
process_id=`ps -ef | grep $name | grep -v grep | awk '{ print $2 }'`
if [ $process_id ]
then
kill -9 $process_id
fi
done
echo " Script Execution completed"
When tomcat shutdowns gracefully there is no issue. But when tomcat doesn't stop, I'm having issues.
Below peice of code when ran directly on command prompt gives me the correct process ID(62457) of tomcat. But the same peice of in shell script is giving me three process ID's(62610,62611,62457).
process_id=`ps -ef | grep $name | grep -v grep | awk '{ print $2 }'`
can you let me know why I'm getting three process ID's in the script compared to just one ?
Any other easier suggestion to KILL ?