Kind of like trying to remember all three Starsky And Hutch theme tunes in one sitting; by the time you get to the end of humming theme number two, your grasp on theme number three, which seemed firm at the outset of humming theme number one, is already slipping inexorably through your fingers. Your brain-fingers.
So after a decade of forgetting, or making notes in forgotten wikis, I'm putting it here so I can sit back and forget it's even here.
I need to remember to set the necessary environment variables first, start/stop/check/kill the tomcat processes and set the run-levels, amongst other things.
Here it is, and if anyone finds it, I hope it helps. Any good tweaks would be appreciated.
Possible improvements include use of PID files instead of a grep/pgrep to determine the state of running processes, although pitfalls lie in wait down that path too.
It can be modified to cope with multi-homed tomcat installs fairly trivially, by finding the relevant process-unique grep terms.
#!/bin/sh
### BEGIN INIT INFO
# Provides: tomcat7
# Required-Start: $local_fs $network
# Required-Stop: $local_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Example initscript for tomcat 7 with single base
### END INIT INFO
JAVA_HOME=/home/youruser/opt/jre1.7.0_09
JAVA_OPTS="-Duser.language=en -Duser.region=GB"
CATALINA_HOME=/home/youruser/apache-tomcat-7.0.32
CATALINA_OPTS="-Xmx900m -Xms128m"
TOMCAT_USER=youruser
export JAVA_HOME CATALINA_HOME CATALINA_OPTS JAVA_OPTS
start_tomcat=$CATALINA_HOME/bin/startup.sh
stop_tomcat=$CATALINA_HOME/bin/shutdown.sh
tomcat_procs() {
pgrep -u $TOMCAT_USER -f $CATALINA_HOME
}
start() {
# check if tomcat is already running
if [ `tomcat_procs | wc -l` -gt 0 ]; then
echo Tomcat is already running? Investigate process $(tomcat_procs)
exit 1
fi
echo Starting tomcat:
su -c ${start_tomcat} $TOMCAT_USER
echo Tomcat is running with the following process: $(tomcat_procs)
}
stop() {
# check it is actually running
if [ `tomcat_procs | wc -l` -eq 0 ]; then
echo Tomcat is already stopped!
exit 1
fi
echo Shutting down tomcat:
${stop_tomcat}
echo "issued stop command - waiting for process $(tomcat_procs) to end"
for i in {1..60}
do
sleep 1
procs=`tomcat_procs | wc -l`
if [ $procs -eq 0 ]; then
echo && echo Stopped
break
elif [ $procs -eq 1 ]; then
if [ $i -lt 55 ]; then
echo -n .
else
echo && echo exhausted allocated wait time - killing outstanding tomcat process... $(tomcat_procs)
pkill -u $TOMCAT_USER -f $CATALINA_HOME
fi
else
echo Tomcat is running more than once? Check it out! $(tomcat_procs)
break
fi
done
echo done.
}
status() {
procs=`tomcat_procs | wc -l`
if [ $procs -eq 0 ]; then
echo Tomcat is stopped
elif [ $procs -eq 1 ]; then
echo Tomcat is running with the following process: $(tomcat_procs)
else
echo Tomcat is running more than once? Investigate! $(tomcat_procs)
fi
}
# See how we were called
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
stop
sleep 5
start
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
esac
exit 0
Edit this script to adjust the pathnames and usernames for your own circumstances. Then place it in /etc/init.d, make sure it has the execute bit set, and then use the appropriate system command to install the links into the desired runlevel directories. Since this is a fairly late starting process, something like start priority 98, stop priority 02 should be good. You can make the links by hand, but using the built in tools is usually the safest bet: e.g. on Debian derivatives such as Ubuntu
# update-rc.d tomcat7 defaults 98 02
You can man insserv or update-rc.d for useful info on the LSB dependency information. Or, on RH derivatives, something like
# chkconfig --add tomcat7
# chkconfig tomcat7 on
Now, all you have to do is set up a Linux Container (LXC) to be running your tomcat instance inside, and you'll be spreading some service goodness all around your organisation.
No comments:
Post a Comment