centos7 开机自启jar 。亲测有效

vi /usr/lib/systemd/system/xx.service

[Unit]
Description=box-api
After=network.target

[Service]
Type=forking
ExecStart=/home/www/xx-start.sh
ExecStop=/home/www/xx-stop.sh
PrivateTmp=true

User=root
Group=root

[Install]
WantedBy=multi-user.target

 

 

xx-start.sh 内容以下.

#!/bin/bash
SpringBoot=/home/www/boxapis-0.0.1-SNAPSHOT.jar
log=/home/www/log.log

export JAVA_HOME=/usr/local/jdk1.8.0_151
export PATH=$JAVA_HOME/bin:$PATH

count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`
if [ $count != 0 ];then
echo "$SpringBoot is running..."
else
echo "Start $SpringBoot success..."
nohup java -Xms128m -Xmx512m -jar $SpringBoot > $log 2>&1 &
fi

 

 

xx-stop.sh 内容以下

#!/bin/bash
SpringBoot=/home/www/boxapis-0.0.1-SNAPSHOT.jar
log=/home/www/log.log


echo "Stop $SpringBoot"
boot_id=`ps -ef |grep java|grep $SpringBoot|grep -v grep|awk '{print $2}'`
count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`

if [ $count != 0 ];then
kill $boot_id
count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`

boot_id=`ps -ef |grep java|grep $SpringBoot|grep -v grep|awk '{print $2}'`
kill -9 $boot_id
rm -rf $log
fi

 

记得给这两个文件加上权限

chmod 777 xx-start.sh

chmod 777 xx-stop.sh

 

systemctl daemon-reload

加入开机自启动

# systemctl enable nginx.service

如何取消开机自启动

# systemctl disable nginx.service

测试。

systemctl start xx-service

 

posted @ 2021-08-23 15:34  lenton  阅读(467)  评论(0编辑  收藏  举报