Automatic Tomcat Startup in Linux Upon System Boot

  • Last update:June 24, 2025
  • Overview

    This document introduces how to configure Tomcat installed in Linux to be automatically started up upon system boot.

    iconNote:
     Since the path of log files will change after you configure Tomcat to be automatically started up upon system boot, you are advised to specify a path for log files. 

    Configuring Tomcat for Automatic Startup upon System Boot with Ubuntu16.04 and the Previous &CentOS6.x&RedHat6.x

    Creating the Tomcat Management Script

    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_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 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

    Setting Tomcat to Be Automatically Started up upon System Boot with chkconfig

    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.

    Configuring Tomcat for Automatic Startup upon System Boot with Ubuntu16.10 and the Later &CentOS7.x&RedHat7.x

    Creating the Tomcat Management Script

    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 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
    iconNote:
    You need to modify the installation path /usr/tomcat of Tomcat in the script according to the actual path.

    2. After saving the script file, set the execution permission of the file.

    chmod a+x /usr/lib/systemd/system/tomcat.service

    Specifying the JRE Path of Tomcat Startup

    1. Edit the setclasspath.sh file in the bin directory of Tomcat.

    vi /usr/tomcat/bin/setclasspath.sh   #Your own Tomcat path
    iconNote:
    Normally, the file has content. If the file is empty, edit Tomcat/bin/setenv.sh. If the file does not exist, you can create a setenv.sh file.

    2. Add a 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

    The following figure shows the effect after you add the JRE environment variable.

    Save the file and exit the editing page.

    Setting Tomcat to Be Automatically Started up upon System Boot with systemctl

    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.

    附件列表


    主题: Deployment and Integration
    • Helpful
    • Not helpful
    • Only read

    滑鼠選中內容,快速回饋問題

    滑鼠選中存在疑惑的內容,即可快速回饋問題,我們將會跟進處理。

    不再提示

    10s後關閉

    Get
    Help
    Online Support
    Professional technical support is provided to quickly help you solve problems.
    Online support is available from 9:00-12:00 and 13:30-17:30 on weekdays.
    Page Feedback
    You can provide suggestions and feedback for the current web page.
    Pre-Sales Consultation
    Business Consultation
    Business: international@fanruan.com
    Support: support@fanruan.com
    Page Feedback
    *Problem Type
    Cannot be empty
    Problem Description
    0/1000
    Cannot be empty

    Submitted successfully

    Network busy