Overview
This document introduces how to configure Tomcat installed in Linux to start automatically upon system boot.
Note: Configuring Automatic Tomcat Startup upon System Boot for Ubuntu 16.04 and Earlier Releases, CentOS 6.x, and RedHat 6.x
Creating a Script to Manage Tomcat
1. First, create a tomcat script in the /etc/init.d/ directory.
vi /etc/init.d/tomcat
Press the i (lowercase) key to enter insert mode, and add the following content to the script.
#!/bin/bash
# tomcat startup script for the Tomcat server
# chkconfig: 35 80 20
# description: start the tomcat deamon
#prog=tomcat
#EDISPORT=8080
#The default port number is 8080. If a non-default port is used, modify it accordingly.
#EXEC=/usr/tomcat/bin/startup.sh
#Path to the Tomcat startup script
#CONF="/usr/tomcat/bin/catalina.sh"
#Path to the configuration script
#<---------------jdk--------------->#
#. /etc/rc.d/init.d/functions
#prog=tomcat
#JAVA_HOME=/usr/jdk/jdk1.8.0_181
#export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:.
#export PATH=$PATH:$JAVA_HOME/bin
#export JAVA_HOME#CATALANA_HOME=/usr/tomcat/
#export CATALINA_HOME
#<---------------jdk--------------->#
CATALANA_HOME=/usr/tomcat/
export CATALINA_HOME
case "$1" in start)
echo "Starting Tomcat..."
$CATALANA_HOME/bin/startup.sh
;;
stop)
echo "Stopping Tomcat..."
$CATALANA_HOME/bin/shutdown.sh
;;
restart)
echo "Stopping Tomcat..."
$CATALANA_HOME/bin/shutdown.sh
sleep 2
echo
echo "Starting Tomcat..."
$CATALANA_HOME/bin/startup.sh
;;
*)
echo "Usage: $prog {start|stop|restart}"
;;
esac exit 0
For Tomcat installed by custom compilation (as provided in this document), you need to modify the following configuration items in the script according to your installation path.
CATALANA_HOME=/usr/tomcat/ # Root directory of catalina.sh under tomcat/bin/
2. After saving the script file, set the execution permission of the file.
chmod a+x /etc/init.d/tomcat
3. You can now manage the Tomcat service using this script.
/etc/init.d/tomcat start
/etc/init.d/tomcat stop
Enabling Automatic Tomcat Startup upon System Boot Using chkconfig
1. After completing the steps above to manage the Tomcat service using the script, you can use the chkconfig command to enable automatic Tomcat startup upon system boot.
Add the Tomcat service to the chkconfig management list:
chkconfig --add /etc/init.d/tomcat
2. After configuring the script, you can set automatic startup upon system boot and manage the service using the following commands.
chkconfig tomcat on # Enable automatic startup on system boot.
chkconfig tomcat off # Disable automatic startup on system boot.
service tomcat start # Start the Tomcat service.
service tomcat stop # Stop the Tomcat service.
service tomcat restart # Restart the Tomcat service.
Configuring Automatic Tomcat Startup upon System Boot for Ubuntu 16.10 and Later Releases, CentOS 7.x, and RedHat 7.x
Creating a Script to Manage Tomcat
1. Create a tomcat.service file in the /usr/lib/systemd/system/ directory.
vi /usr/lib/systemd/system/tomcat.service
Press the i (lowercase) key to enter insert mode, and add the following content to the script.
[Unit]
Description=tomcat service
After=network.target
[Service]
Type=forking
ExecStart=/usr/tomcat/bin/startup.sh
ExecReload=/usr/tomcat/bin/startup.sh -s reload
ExecStop=/usr/tomcat/bin/shutdown.sh
PrivateTmp=true
[Install]WantedBy=multi-user.target
Note: 2. After saving the script file, set the execution permission of the file.
chmod a+x /usr/lib/systemd/system/tomcat.serviceSpecifying the JRE Path for Tomcat Startup
1. Edit the setclasspath.sh file in the bin directory of Tomcat.
vi /usr/tomcat/bin/setclasspath.sh #Your own Tomcat path
Note:2. Add the JRE environment variable.
export JAVA_HOME=/usr/local/java/jdk1.8.0_151 #Your own Java path
export JRE_HOME=/usr/local/java/jdk1.8.0_151/jre #Your own JRE path
Note: export JAVA_HOME=/usr/local/tomcat/jdk/
export PATH=$JAVA_HOME/bin:$PATH
The following figure shows the effect after you add the JRE environment variable.

3. Save the file and exit the editing page.
Enabling Automatic Tomcat Startup upon System Boot Using systemctl
2. After configuring the script, you can set automatic startup upon system boot and manage the service using the following commands.
systemctl enable tomcat.service # Enable automatic startup on system boot.
systemctl disable tomcat.service # Disable automatic startup on system boot.
systemctl start tomcat.service # Start the Tomcat service.
systemctl stop tomcat.service # Stop the Tomcat service.
systemctl status tomcat.service # Check the current status of the service.
systemctl list-units --type=service # Check all active services.