This document introduces how to configure Tomcat installed in Linux to be automatically started up upon system boot.
1. First, create a tomcat file in /etc/init.d/ in the system.
vi /etc/init.d/tomcat
Press the I key in lowercase 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 port number is 8080 by default. Modify the port number to the actual port number if you do not use the default one.#EXEC=/usr/tomcat/bin/startup.sh#Startup path of the Tomcat container#CONF="/usr/tomcat/bin/catalina.sh"#Path of configuration files#<---------------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_HOMEcase "$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.shsleep 2echoecho "Starting Tomcat..."$CATALANA_HOME/bin/startup.sh;;*)echo "Usage: $prog {start|stop|restart}";;esacexit 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 paths.
CATALANA_HOME=/usr/tomcat/ ====> the root directory of catalana.sh in tomcat/bin/
2. After saving the script file, set the execution permission of the file.
chmod a+x /etc/init.d/tomcat
3. Then, you can manage the Tomcat service by the script.
/etc/init.d/tomcat start/etc/init.d/tomcat stop
1. Through the above-mentioned steps, you have realized the function of managing the Tomcat service by the script. Next, you can configure Tomcat for automatic startup upon system boot with chkconfig.
First, add the Tomcat service to the chkconfig management list:
chkconfig --add /etc/init.d/tomcat
2. After configuration, you can use the following command to set operations, for example, automatic startup upon system boot.
chkconfig tomcat on # Set automatic startup upon system boot.chkconfig tomcat off # Stop automatic startup upon system boot.service tomcat start # Start the Tomcat service.service tomcat stop # Stop the Tomcat service.service tomcat restart # Restart the Tomcat service.
1. First, create a tomcat.service file in /usr/lib/systemd/system/ in the system.
vi /usr/lib/systemd/system/tomcat.service
Press the I key in lowercase mode, and add the following content to the script:
[Unit]Description=tomcat serviceAfter=network.target[Service]Type=forkingExecStart=/usr/tomcat/bin/startup.shExecReload=/usr/tomcat/bin/startup.sh -s reloadExecStop=/usr/tomcat/bin/shutdown.shPrivateTmp=true[Install]WantedBy=multi-user.target
chmod a+x /usr/lib/systemd/system/tomcat.service
1. Edit the setclasspath.sh file in the bin directory of Tomcat.
vi /usr/tomcat/bin/setclasspath.sh #Your own Tomcat path
2. Add a JRE environment variable.
export JAVA_HOME=/usr/local/java/jdk1.8.0_151 #Your own java pathexport JRE_HOME=/usr/local/java/jdk1.8.0_151/jre #Your own JRE path
The following figure shows the effect after you add the JRE environment variable.
Save the file and exit the editing page.
After configuring the script, you can use the following command to set operations, for example, automatic startup upon system boot.
systemctl enable tomcat.service # Set automatic startup upon system boot.systemctl disable tomcat.service # Stop automatic startup upon system boot.systemctl start tomcat.service # Start the Tomcat service.systemctl stop tomcat.service # Stop the Tomcat service.systemctl status tomcat.service # View the current state of the service.systemctl list-units --type=service # View all services that have been started.
滑鼠選中內容,快速回饋問題
滑鼠選中存在疑惑的內容,即可快速回饋問題,我們將會跟進處理。
不再提示
10s後關閉
Submitted successfully
Network busy