Thursday, March 15, 2012

Restart Tomcat via Shell Script Using Crontab


Here is the shell script to shutdown and start Tomcat

#!/bin/bash
#Important!! Script Must be run as root(you can ignore this if your tomcat does not need root
PATH=$PATH:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/ant/bin:/opt/java/
export PATH
NOW=$(date +"%Y%m-%dT%H%M")

if [ "$(id -u)" != "0" ];
then
echo "This script must be run as root" 1>&2
exit 1
fi

echo `date` ":=================================" >> /tmp/tomcat-restart.log
echo `date` ":Tomcat restart script initiated!!" >> /tmp/tomcat-restart.log
echo `date` ":=================================" >> /tmp/tomcat-restart.log

if [ `ps -ef | egrep 'tomcat6' | egrep -v egrep | wc -l` -gt 0 ];
then
echo `date` ":Tomcat is running" >> /tmp/tomcat-restart.log

kill -3 `ps ax | grep tomcat6 | grep -v grep | awk '{print $1}'`
/opt/tomcat/bin/catalina.sh stop
echo `date` ":Tomcat has shutdown" >> /tmp/tomcat-restart.log

if [ `ps -ef | egrep 'tomcat6' | egrep -v egrep | wc -l` -gt 0 ];
then
kill -9 `ps ax | grep tomcat6 | grep -v grep | awk '{print $1}'`
fi

#echo "Restarting tomcat !!!!!" >> /tmp/tomcat-restart.log
#/opt/tomcat/bin/catalina.sh start
#else
#echo "Tomcat not found running" >> /tmp/tomcat-restart.log
#echo "Restarting tomcat !!!!!" >> /tmp/tomcat-restart.log
#/opt/tomcat/bin/catalina.sh start
fi

echo `date` "Sleep started" >> /tmp/tomcat-restart.log
sleep 30
echo `date` "Sleep End" >> /tmp/tomcat-restart.log

if [ `ps -ef | egrep 'tomcat6' | egrep -v egrep | wc -l` -eq 0 ];
then
/opt/tomcat/bin/catalina.sh start
echo `date` "Tomcat restarted." >> /tmp/tomcat-restart.log
fi

Notes:
  • `ps -ef | egrep 'tomcat6' | egrep -v egrep | wc -l`  - This returns number of active tomcat6 processors
  • If it is grater than '0', tomcat6 is running and has that number of active processors.
  • If it is '0', tomcat6 is not running
What you need to do:
  • Copy and past this into your text editor and save it as restart-tomcat.sh
  • Copy restart-tomcat.sh into /usr/bin/
  • Go to /usr/bin/ and change the file permission to 0777
    • cd /usr/bin/
    • #chmod 0777  restart-tomcat.sh
  • Change these in  restart-tomcat.sh according to your environment
    • PATH 
    • tomcat6 
    • /opt/tomcat/bin/catalina.sh stop 
    • /opt/tomcat/bin/catalina.sh start 
  • If your crontab does not contains JRE_HOME=/opt/java, script might complain when you run  restart-tomcat.sh. If complains, add it.
  • You can run this script as follows;
    • ./restart-tomcat.sh 
    • This will create a log in  /tmp/tomcat-restart.log
    • If there's any errors, you can find it in /var/spool/mail/root file. Do this to see the errors.
      • cd /var/spool/mail
      • tail -f root
  • If everything goes well, create a crontab record
    • crontab -e (to edit the crontab)
    • This record trigger the job at 10:15 AM everyday
      • 15 10 * * * /usr/bin/restart-tomcat.sh
That's it!

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home