I have written a script to stop a Tomcat instance, remove the .war file and application directory, copy a new .war file from a NFS mount to the webapps directory, and then restart the Tomcat instance. The script works fine, but I am concerned if the Tomcat instance does not terminate for some reason. How do I have the script confirm the daemon has terminated, and if not find the PID and kill it before moving to the next step? An if statement with awk or sed? If so how should it be constructed?
1 Answer
One thing you can do is store the pid of the tomcat in a file then read that file and use the pid in kill command. To store pid of tomcat, add below line in your tomcat's catalina.sh
CATALINA_PID=$CATALINA_HOME/tomcat.pid This will create a file tomcat.pid in your catalina home and store the pid of current process. Then you can just use the command
kill -9 $(cat path_to_catalina_home/tomcat.pid) Other wise just use the command
pkill -9 tomcat_directory_name make sure "tomcat_directory_name" or whatever you pass in pkill command should be unique, other wise you will end up killing multiple processes in your system. You can use the longer version of pkill as well
ps aux | grep tomcat_directory_name | grep -v grep | awk '{print "kill -9 " $2}' | bash -v this will do the same as pkill but a bit safe. As you can test this command until you get the correct pid by removing bash -v in the end.