1. Add startup script in any location
vim xxxx.sh
#!/bin/sh
cd /usr/local/java/ydServer/target #切换到jar包所在目录
pid=`ps -ef|grep 'xxxx.jar*'|grep -v grep|awk '{print $2}'`
if [ -n "${pid}" ];then
kill -9 ${pid}
echo "Process ITSM killed!"
#java 根据自己jdk路径填写, jar包写绝对路径
nohup java -Dfile.encoding=utf-8 -jar xxxx.jar >/xxxx/xxxx.txt 2>&1 &
echo "Restart ITSM Success!"
else
nohup java -Dfile.encoding=utf-8 -jar xxxx.jar >/xxxx/xxxx.txt 2>&1 &
echo "Start ITSM Success!"
fi
2. Authorization script file
chmod 777 xxx.sh
3. Configure service file
vim /etc/systemd/system/xxx.service
[Unit]
Description=xxx-service
After=network.target
[Service]
Type=forking
ExecStart=/xxx/xxxx.sh
[Install]
WantedBy=multi-user.target
4. Set as service
#重载系统服务
systemctl daemon-reload
#测试 服务是否生效
systemctl start demo.service
ps aux|grep demo
#查看jar包运行情况
ps -ef|grep '***.jar'
# 设置服务为开机启动服务
systemctl enable demo.service
#开机服务查询
systemctl list-units --type=service