极VPS,追求极致;
分享VPS技术;

Centos7设置jar开机自启

先创建基本的start、stop、restart脚本:

jarStart.sh脚本:

#!/bin/sh
nohup java -jar /root/myjar-1.0.1-SNAPSHOT.jar > myjar.log 2>&1 &
echo Start Success!

jarStop.sh脚本:

#!/bin/sh
PID=`ps -ef | grep java | grep -v grep | awk '{print $2}'`
if [ "" != "$PID" ]; then
 echo "killing $PID"
 kill -9 $PID
 echo "Java stoped."
else
 echo "Java not running!"
fi

jarRestart.sh脚本:

#!/bin/sh
PID=`ps -ef | grep java | grep -v grep | awk '{print $2}'`
if [ "" != "$PID" ]; then
 echo "killing $PID"
 kill -9 $PID
 echo "Java stoped."
else
 echo "Java not running!"
fi
nohup java -jar /root/myjar-1.0.1-SNAPSHOT.jar > myjar.log 2>&1 &
echo $! > tpid
echo Start Success!

赋予以上sh脚本权限:

chmod +x *.sh

新建开机自启动service脚本,以myjar.service命名:

cd /usr/lib/systemd/system
vi myjar.service

输入以下内容;(start、stop、restart脚本路径要根据自己情况更改)

[Unit]
Description=myjar
After=syslog.target network.target

[Service]
Type=forking
ExecStart=/root/jarStart.sh
ExecReload=/root/jarRestart.sh
ExecStop=/root/jarStop.sh
PrivateTmp=true
User=root
Group=root

[Install]
WantedBy=multi-user.target

赋予777权限:

chmod 777 myjar.service

设置service脚本开机自启动:

systemctl enable myjar.service

其他命令:

# 启动
systemctl start myjar
# 查看运行状态
systemctl status myjar
# 停止
systemctl stop myjar
赞(0)
未经允许不得转载:极VPS » Centos7设置jar开机自启

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址