来源:https://www.cnblogs.com/zengnansheng/p/10404645.html
注意:脚本中的第一行 #!/bin/bash 是必不可少的,而且必须是第一行
cd /usr/lib/systemd/system
新建开机自启脚本 vim zns-article-service.service
#!/bin/sh
[Unit]
Description=zns-article-service
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/apps/zns-article-service/zns-article-service-start.sh
ExecStop=/usr/local/apps/zns-article-service/zns-article-service-stop.sh
PrivateTmp=true
[Install]
WantedBy=multi-user.target
新建一个启动脚本 zns-article-service-start.sh
#!/bin/sh
export JAVA_HOME=/usr/local/software/jdk
export PATH=$JAVA_HOME/bin:$PATH
nohup java -jar /usr/local/apps/zns-article-service/zns-article-service.jar > /logs/zns-article-service.log 2>&1 &
echo $! > /var/run/zns-article-service.pid
新建一个停止脚本 zns-article-service-stop.sh
#!/bin/sh
PID=$(cat /var/run/zns-article-service.pid)
kill -9 $PID
增加权限
chmod +x zns-article-service-start.sh
chmod +x zns-article-service-stop.sh
如果更改项目了:
先运行
systemctl daemon-reload 重新载入修改后配置文件,让设置生效
systemctl status zns-article-service 查看服务状态
systemctl enable zns-article-service 设置开机启动
systemctl disable zns-article-service 禁止开机启动
systemctl start zns-article-service 启动服务
systemctl stop zns-article-service 停止服务
查看项目运行日志
journalctl -u zns-article-service 或 journalctl -u zns-article-service.service