On a production server, we need to configure Tomcat as the system service, that is, a program running at boot without any users' action. If you install Tomcat on Windows, the installer creates the service for you and sets it for an automatic startup.
Are you wondering how to do it on Linux? In this recipe, we are going to configure your Linux box for automatic start of services. We will create a script and learn how it works.
sudo
, as we are going to create a file in a system folder:#!/bin/sh ### BEGIN INIT INFO # Provides: tomcat # Required-Start: $local_fs $remote_fs $network $syslog # Required-Stop: $local_fs $remote_fs $network $syslog # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Start/Stop Tomcat v7.0.42 ### END INIT INFO # # /etc/init.d/tomcat # export JAVA_HOME=/usr/lib/jvm/jre1.6.0_37 export PATH=$JAVA_HOME/bin:$PATH export CATALINA_HOME=/opt/tomcat7042 export JAVA_OPTS="-Djava.awt.headless=true" case $1 in start) sh $CATALINA_HOME/bin/startup.sh ;; stop) sh $CATALINA_HOME/bin/shutdown.sh ;; restart) sh $CATALINA_HOME/bin/shutdown.sh sh $CATALINA_HOME/bin/startup.sh ;; *) echo "Usage: /etc/init.d/tomcat {start|stop|restart}" exit 1 ;; esac exit 0
tomcat
and save it in the /etc/init.d
folder.$ sudo chmod a+x /etc/init.d/tomcat
$ sudo service tomcat Usage: /etc/init.d/tomcat {start|stop|restart}
$ sudo service tomcat start
$ ps -ef | grep java root 2813 1 99 19:06 pts/0 00:00:24 /usr/bin/java -Djava.util.logging.config.file=/opt/Tomcat7042/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/opt/Tomcat7042/endorsed -classpath /opt/Tomcat7042/bin/bootstrap.jar:/opt/Tomcat7042/bin/tomcat-juli.jar -Dcatalina.base=/opt/Tomcat7042 -Dcatalina.home=/opt/Tomcat7042 -Djava.io.tmpdir=/opt/Tomcat7042/temp org.apache.catalina.startup.Bootstrap start
$ sudo service tomcat stop
update-rc
as follows:$ sudo update-rc.d tomcat defaults Adding system startup for /etc/init.d/tomcat ... /etc/rc0.d/K20tomcat -> ../init.d/tomcat /etc/rc1.d/K20tomcat -> ../init.d/tomcat /etc/rc6.d/K20tomcat -> ../init.d/tomcat /etc/rc2.d/S20tomcat -> ../init.d/tomcat /etc/rc3.d/S20tomcat -> ../init.d/tomcat /etc/rc4.d/S20tomcat -> ../init.d/tomcat /etc/rc5.d/S20tomcat -> ../init.d/tomcat
We created a shell script to start Apache Tomcat. Now, as you boot your Linux machine, Tomcat will be initialized and all the web application contained will be available for user requests. If you prefer to manually start and stop Tomcat, the script could be useful for you. Just create it as described and avoid the last step. You will use the script to start or stop Tomcat from the command line, that is, sudo service tomcat start
or sudo service tomcat stop
.
3.146.176.145