In previous post we have setup Tomcat to run its multiple instances now we want that tomcat should start automatically on server reboots. This can be achieved by adding tomcat service script.
Introduction
This document will teach us how to setup Tomcat to run as a service (startup when booted) on Linux.
Intended Audience: System admins.
Instructions to setup Tomcat to run as a service (startup when booted) on Linux.
It is actually easy and will be presented step by step.
1. Save tomcat start / stop script
Copy and paste the following script into text editor:
# This is the init script for starting up the # Jakarta Tomcat server # # chkconfig: 345 91 10 # description: Starts and stops the Tomcat daemon. # # Source function library. . /etc/rc.d/init.d/functions # Get config. . /etc/sysconfig/network # Check that networking is up. [ "${NETWORKING}" = "no" ] && exit 0 USER=tomcat tomcat=/opt/tomcat startup="su - $USER -c $tomcat/bin/startup.sh" shutdown="su - $USER -c $tomcat/bin/shutdown.sh" pidfile=/var/run/$USER.pid lockfile=/var/lock/subsys/$USER start(){ echo -n $"Starting Tomcat service: " #daemon -c $startup pid=`ps -eaf | grep java | grep -v grep |grep $USER | awk '{FS=" "} {print $2}'` echo $pid > ${pidfile} RETVAL=$? echo [ $RETVAL = 0 ] && touch ${lockfile} } stop(){ echo -n "Stopping Tomcat service: " $shutdown killproc -p ${pidfile} $USER RETVAL=$? echo [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile} } restart(){ stop start } # See how we were called. case "$1" in start) start ;; stop) stop ;; status) status -p ${pidfile} $USER ;; restart) restart ;; *) echo $"Usage: $0 {start|stop|status|restart}" exit 1 esac exit $RETVAL
Edit the lines that start with tomcat and USER to match where we installed Tomcat and the user it will run as.
2. Save to /etc/init.d and chmod
Save the edited file above to /etc/init.d directory as “tomcat” (at least on most newer releases since /etc/init.d is a standard now). Then we have to allow execute access to the script, so we will run:
chmod +x tomcat
3. Add to appropriate run level directories The easy way to do this is to just simply run:
chkconfig –add tomcat
And that’s it. Now our Tomcat will start automatically when server is rebooted.
Pingback: Setting Tomcat to run Mutiple instances of it | Mohan Cheema's Online Diary