记项目组需要升级服务器内存,咔咔把服务器全部停掉,升级完之后,我人傻了,不会自启动,连夜干到3点多挨个服务器启动
设置Tomcat为开机启动项
按照上面的方式启动Tomcat,如果我们的虚拟机或者服务器关闭了,重启服务器后Tomcat是关闭的,但是我们希望虚拟机或者服务器重启后,Tomcat可以自己启动,所以我们需要设置Tomcat为开机启动项
创建setenv.sh文件,为Tomcat添加启动参数
catalina.sh在执行的时候会调用同级路径下的setenv.sh来设置额外的环境变量,因此在/usr/local/tomcat/apache-tomcat-8.5.60/bin路径下创建setenv.sh文件,内容如下:
# 设置Tomcat的PID文件
CATALINA_PID="$CATALINA_BASE/tomcat.pid"
# 添加JVM选项
JAVA_OPTS="-server -XX:PermSize=256M -XX:MaxPermSize=1024m -Xms512M -Xmx1024M -XX:MaxNewSize=256m"
在/usr/local/tomcat/apache-tomcat-8.5.60/bin/catalina.sh文件开头添加JAVA_HOME和JRE_HOME,其中/usr/local/jdk1.8.0_152为jdk的安装目录
export JAVA_HOME=/usr/local/jdk1.8.0_152
export JRE_HOME=/usr/local/jdk1.8.0_152/jre
如果在catalina.sh不配置JAVA_HOME和JRE_HOME就会报如下的错误(本人测试不需要配置也可)
[root@JourWon ~]# systemctl status tomcat
● tomcat.service - Tomcat
Loaded: loaded (/usr/lib/systemd/system/tomcat.service enabled vendor preset: disabled)
Active: failed (Result: exit-code) since Mon 2019-10-21 19:54:54 CST 6s ago
Process: 8746 ExecStart=/usr/local/tomcat/apache-tomcat-8.5.47/bin/startup.sh (code=exited, status=1/FAILURE)
Oct 21 19:54:54 JourWon systemd[1]: Starting Tomcat...
Oct 21 19:54:54 JourWon startup.sh[8746]: Neither the JAVA_HOME nor the JRE_...d
Oct 21 19:54:54 JourWon startup.sh[8746]: At least one of these environment ...m
Oct 21 19:54:54 JourWon systemd[1]: tomcat.service: control process exited,...=1
Oct 21 19:54:54 JourWon systemd[1]: Failed to start Tomcat.
Oct 21 19:54:54 JourWon systemd[1]: Unit tomcat.service entered failed state.
Oct 21 19:54:54 JourWon systemd[1]: tomcat.service failed.
Hint: Some lines were ellipsized, use -l to show in full.
在/usr/lib/systemd/system路径下添加tomcat.service文件,内容如下:
[Unit]
Description=Tomcat
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
TimeoutSec=0
PIDFile=/usr/local/tomcat/apache-tomcat-8.5.60/tomcat.pid
ExecStart=/usr/local/tomcat/apache-tomcat-8.5.60/bin/startup.sh
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
此外service文件修改后需要调用systemctl daemon-reload
命令重新加载。
配置TimeoutSec=0的目的是让开机启动不处理tomcat启动超时,保证tomcat耗时过长时不会被系统terminating,如果不配置可能出现下面的情况
Oct 21 20:26:37 JourWon startup.sh[1634]: Existing PID file found during start.
Oct 21 20:26:37 JourWon startup.sh[1634]: Removing/clearing stale PID file.
Oct 21 20:26:37 JourWon startup.sh[1634]: Tomcat started.
Oct 21 20:26:37 JourWon systemd[1]: PID file /usr/local/tomcat/apache-tomcat-8.5.47/tomcat.pid not readable (yet?) after start.
Oct 21 20:26:38 JourWon polkitd[464]: Unregistered Authentication Agent for unix-process:1628:19013 (system bus name :1.23, object path /org/freedesktop/PolicyKit1/AuthenticationAgent, loca
Oct 21 20:28:07 JourWon systemd[1]: tomcat.service start operation timed out. Terminating.
Oct 21 20:28:07 JourWon systemd[1]: Failed to start Tomcat.
把Tomcat加入开机自启动
systemctl enable tomcat.service
重启服务器
reboot
再次连接后,查看服务状态
[root@JourWon ~]# systemctl status tomcat
● tomcat.service - Tomcat
Loaded: loaded (/usr/lib/systemd/system/tomcat.service enabled vendor preset: disabled)
Active: activating (start) since Mon 2019-10-21 20:12:19 CST 8s ago
Process: 9244 ExecStart=/usr/local/tomcat/apache-tomcat-8.5.47/bin/startup.sh (code=exited, status=0/SUCCESS)
CGroup: /system.slice/tomcat.service
└─9255 /usr/local/jdk1.8.0_152/jre/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/apache-tomcat-8.5.47/conf/logging.properties -Djava.util.logging.manager=org.apac...
Oct 21 20:12:19 JourWon systemd[1]: Starting Tomcat...
Oct 21 20:12:19 JourWon startup.sh[9244]: Existing PID file found during start.
Oct 21 20:12:19 JourWon startup.sh[9244]: Removing/clearing stale PID file.
Oct 21 20:12:19 JourWon startup.sh[9244]: Tomcat started.
Oct 21 20:12:19 JourWon systemd[1]: PID file /usr/local/tomcat/apache-tomcat-8.5.47/tomcat.pid not readable (yet?) after start.
查看开机启动列表命令
systemctl list-unit-files | grep enabled
查看Tomcat是否设置为开机启动项,如果显示为enabled,说明设置成功
参数说明
- static:表示该服务与其他服务相关联,不能单独设置该服务的启动状态
- disabled:表示禁止开机启动
- enabled:表示允许开机启动
[root@JourWon ~]# systemctl list-unit-files | grep tomcat
tomcat.service enabled
评论区