How to check for and start the Tomcat using a script

Hello Everyone,.

I am a novice with shell scripting and have written some minor shell scripts to copy files and such that. Now I have a requirement to write a shell script to go check if Tomcat running in the same server is up or not, if not then run the Tomcat startup script. Then put this script in a cron. At this point I have few ideas but I just don't know how to bind them into a running script or even know if its the right direction to follow.

I would very much appreciate if some one can guide me through this. Any suggestions and ideas would be greatly appreciated.

Thanks in Advance.

bhaire we need input, how could we check a running tomcat process? lets say in order to check a java process which is running on 38080 port we can:

ps -ef | grep java | grep 38080

how do you start tomcat? Like below? you should provide us such input for checking things.

cd $CATALINA_HOME/bin
      ./startup.sh  

Eagle,

Thanks. I wanted was to create a shell script that will be put in a cronjob to run every say 5 hours daily and the script will check if the Tomcat is up or not if not then start it automatically without having to do it manually.

Going your way, as you pointed how to make a shellscrpit read the contents of "ps -ef|grep java" and figure out if Tomcat is up or not if not then run "./startup.sh"

any help would be appreciated.

Thanks

Sorry for late reply,

I hope it'll give you an idea to do it. I checked a production machine which is working over tomcat but i coudlnt be sure what is the right command to see that tomcat is up? but you might do it a few ways.

  1. Tomcat's default port is 8080. u can grep it and use port status in comparision loop.
#!/bin/bash
STAT=`netstat -na | grep 8080 | awk '{print $7}'`
if [ "$STAT" = "LISTEN" ]; then
echo "DEFAULT TOMCAT PORT IS LISTENING, SO ITS OK"
elif [ "$STAT" = "" ]; then 
echo "8080 PORT IS NOT IN USE SO TOMCAT IS NOT WORKING"
## only if you defined CATALINA_HOME in JAVA ENV ##
cd $CATALINA_HOME/bin
./startup.sh
fi
RESULT=`netstat -na | grep 8080 | awk '{print $7}' | wc -l`
if [ "$RESULT" = 0 ]; then
echo "TOMCAT PORT STILL NOT LISTENING"
elif [ "$RESULT" != 0 ]; then
echo "TOMCAT PORT IS LISTENINS AND SO TOMCAT WORKING"
fi
  1. You can grep something more reliable than a port number and then you can modify the script according to that data. Maybe you grep "catalina" etc..

Thanks a bunch!!!