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 start xx-service

 

centos7 开机自启jar 。亲测有效

上一篇:spring-boot 2.5.4,nacos 作为配置、服务发现中心,Cloud Native Buildpacks 打包镜像,GitLab CI/CD


下一篇:Jmeter线程组设置