您的当前位置:首页正文

linux 设置tomcat开机启动

2024-11-08 来源:个人技术集锦

tomcat 开机自启两种方式:

方法一:

1. 添加文件
vi /etc/systemd/system/tomcat.service

添加内容

[Unit]
Description=Tomcat8
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=oneshot
ExecStart=/usr/local/apache-tomcat-8.5.40/bin/startup.sh
ExecStop=/usr/local/apache-tomcat-8.5.40/bin/shutdown.sh
ExecReload=/bin/kill -s HUP $MAINPID
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
2. 进入目录
 cd /etc/systemd/system/
3. 设置开机启动
systemctl enable tomcat.service
4. 查看当前tomcat状态
systemctl status tomcat.service
5. 关闭开机自启动
systemctl disable tomcat.service
6. tomcat添加jdk路径,否则自启动会报错 Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
vi /usr/local/apache-tomcat-8.5.40/bin/catalina.sh

export JAVA_HOME=/usr/local/jdk8
export JRE_HOME=/usr/local/jdk8/jre

方法二:

1. 编辑启动脚本: vim /usr/local/auto_run.sh
/usr/local/apache-tomcat-8.5.40/bin/startup.sh
2. 添加 crontab 任务

crontab -e

@reboot /usr/local/auto_run.sh

保存即可

重启服务器,测试
reboot
显示全文