在/usr/lib/systemd/system目录下增加tomcat8.service文件,内容如下:
vi /usr/lib/systemd/system/tomcat8.service
[Unit]
Description=Tomcat
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
或者在tomcat/bin/catalina.sh中添加CATALINA_PID="$CATALINA_BASE/tomcat.pid"
PIDFile=/usr/tomcat/apache-tomcat-8.5.50/tomcat.pid
Environment="JAVA_HOME=/usr/java/jdk1.8.0_241"
ExecStart=/usr/tomcat/apache-tomcat-8.5.50/bin/startup.sh
ExecStop=/usr/tomcat/apache-tomcat-8.5.50/bin/shutdown.sh
ExecReload=/bin/kill -s HUP $MAINPID
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
————————————————————————————————————
如果[Service]节不加PIDFile=/usr/tomcat/apache-tomcat-8.5.50/tomcat.pid,
应该在bin/catalina.sh中添加CATALINA_PID="$CATALINA_BASE/tomcat.pid"
Copy CATALINA_BASE from CATALINA_HOME if not already set
[ -z "$CATALINA_BASE" ] && CATALINA_BASE="$CATALINA_HOME"
设置pid。一定要加在CATALINA_BASE定义后面,要不然pid会生成到/下面
CATALINA_PID="$CATALINA_BASE/tomcat.pid"
————————————————————————————————————
重新加载一下配置。每次改动后也要重新加载
systemctl daemon-reload
//执行systemctl enable tomcat使tomcat8随开机启动
systemctl enable tomcat8.service
设置允许开机自启动
systemctl enable tomcat8
启动服务
systemctl start tomcat8
查看状态
systemctl status tomcat8
查看服务是否激活
systemctl is-active tomcat8
其它命令
禁止开机启动
systemctl disable tomcat8
停止服务
systemctl stop tomcat8
重启服务
systemctl restart tomcat8
查看所有已启动的服务
systemctl list -units --type=service
nginx.service自启动
vi /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/opt/nginx/sbin/nginx
ExecReload=/opt/nginx/sbin/nginx reload
ExecStop=/opt/nginx/sbin/nginx quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target