温馨提示×

linux部署tomcat能自动启动吗

小樊
81
2024-12-27 19:08:40
栏目: 智能运维

是的,在Linux上部署Tomcat后,它可以自动启动

  1. 使用systemd服务:

创建一个新的systemd服务文件,例如/etc/systemd/system/tomcat.service,并添加以下内容:

[Unit]
Description=Apache Tomcat Web Application Container
After=network.target

[Service]
Type=forking

Environment=JAVA_HOME=/path/to/your/java/home
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'

ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh

User=tomcat
Group=tomcat
UMask=0007
RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target

/path/to/your/java/home替换为您的Java安装路径,并根据需要修改其他配置。然后,运行以下命令启用并启动Tomcat服务:

sudo systemctl daemon-reload
sudo systemctl enable tomcat
sudo systemctl start tomcat

现在,每次系统启动时,Tomcat都会自动启动。

  1. 使用SysVinit脚本:

/etc/init.d/目录下创建一个新的脚本文件,例如tomcat,并将以下内容粘贴到其中:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          tomcat
# Required-Start:    $local_fs $remote_fs $network $syslog $named
# Required-Stop:     $local_fs $remote_fs $network $syslog $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Description:       Apache Tomcat Web Application Container
### END INIT INFO

JAVA_HOME=/path/to/your/java/home
CATALINA_PID=/opt/tomcat/temp/tomcat.pid
CATALINA_HOME=/opt/tomcat
CATALINA_BASE=/opt/tomcat
CATALINA_OPTS="-Xms512M -Xmx1024M -server -XX:+UseParallelGC"
JAVA_OPTS="-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom"

case "$1" in
    start)
        /opt/tomcat/bin/startup.sh
        ;;
    stop)
        /opt/tomcat/bin/shutdown.sh
        ;;
    restart)
        /opt/tomcat/bin/shutdown.sh
        /opt/tomcat/bin/startup.sh
        ;;
    *)
        echo "Usage: /etc/init.d/tomcat {start|stop|restart}"
        exit 1
        ;;
esac

exit 0

/path/to/your/java/home替换为您的Java安装路径,并根据需要修改其他配置。然后,运行以下命令使脚本可执行并启动Tomcat服务:

sudo chmod +x /etc/init.d/tomcat
sudo service tomcat start

现在,每次系统启动时,Tomcat都会自动启动。 若要停止或重启Tomcat服务,请分别运行sudo service tomcat stopsudo service tomcat restart命令。

0