Ubuntu 18.04 tomcat 开机自启

1.在tomcat目录下新建文件tomcat.pid 命令如下

touch tomcat.pid

2.在tomcat/bin目录下新建setenv.sh 写入如下命令

#tomcat启动pid

export CATALINA_HOME=/usr/local/java/apache-tomcat-7.0.100

export CATALINA_BASE=/usr/local/java/apache-tomcat-7.0.100

#$CATALINA_BASE为tomcat安装的目录路径,将tomcat.pid指给了CATALINA_PID

CATALINA_PID="$CATALINA_BASE/tomcat.pid"


3.在/lib/systemd/system/创建tomcat.service文件,写入如下命令:

[Unit]

Description=Tomcat

After=network.target

[Service]

Type=forking

PIDFile=/usr/local/java/apache-tomcat-7.0.100/tomcat.pid

ExecStart=/usr/local/java/apache-tomcat-7.0.100/bin/catalina.sh start

ExecReload=/usr/local/java/apache-tomcat-7.0.100/bin/catalina.sh restart

ExecStop=/usr/local/java/apache-tomcat-7.0.100/bin/catalina.sh stop

[Install]

WantedBy=multi-user.target

4.启动、重启、停止

启动tomcat

systemctl start tomcat

重启tomcat

systemctl restart tomcat

停止tomcat

systemctl stop tomcat

5.如果使用systemctl命令可以正常启动tomcat,表示配置没有问题了,下面下加入自启项即可!

加入开机启动

systemctl enable tomcat

禁止开机启动

systemctl disable tomcat

推荐阅读更多精彩内容