1、添加启动脚本,位置任意
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、授权脚本文件
chmod 777 xxx.sh
3、配置service文件
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、设置为服务
#重载系统服务
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