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 status zns-article-service
systemctl enable zns-article-service
systemctl disable zns-article-service
systemctl start zns-article-service
systemctl stop zns-article-service