How to Add Tomcat as a Linux Service and Enable Startup on Boot

Create the relevant entries in the startup directory.
cd /etc/init.d
vi /etc/init.d/tomcat
Using the Tomcat installation directory /usr/local/tomcat/ as an example, fill in the code as follows:
 
#!/bin/sh
#chkconfig: 2345 10 90
# description: Starts and Stops the Tomcat daemon.
##############################################
#Startup script for Tomcat on Linux
 
#filename tomcat.sh
 
#Make sure the java and the tomcat installation path has been added to the PATH
JAVA_HOME=/usr/java/jdk1.8.0_77 
CATALINA_HOME=/usr/local/tomcat   
export JAVA_HOME
export CATALINA_HOME
 
###############################################
start_tomcat=/usr/local/tomcat/bin/startup.sh             
stop_tomcat=/usr/local/tomcat/bin/shutdown.sh               
start() {                                                              
        echo -n "Starting tomcat: "
        ${start_tomcat}
        echo "tomcat start ok."
}
stop() {
        echo -n "Shutting down tomcat: "
        ${stop_tomcat}
        echo "tomcat stop ok."
}
# See how we were called
                                                   
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        stop
        sleep 10
        start
        ;;
  *)
        echo "Usage: $0 {start|stop|restart}"
esac
exit 0
Note: For JAVA_HOME=/usr/java/jdk1.8.0_77, please adjust the JDK version and path according to the version installed on your system. The method is:

ls /usr/java/

Grant script permissions:

chmod 755 tomcat
Add to services:
chkconfig –add tomcat
Enable startup at boot:
chkconfig –level 345 tomcat on
          Done. Now you can start Tomcat using the command service tomcat start. Commands for stopping and restarting the service are similar, simply replace start with stop or restart.

Attachment download link https://www.cnop.net/uploadfile/2016/0805/20160805022735452.zip

Leave a Comment

Your email address will not be published.