Linux中jar包快捷启动、停止、jar包后台运行和 jar包开机自启动 的实现方式
程序员文章站
2022-05-01 21:09:08
...
开机自动执行命令:
注:将/usr/local/project/access/access.jar
设置为快捷启动和自启动
一、设置启动连接
在``/usr/local/project/access/access/bin`目录下新建启动脚本和停止脚步
-
新建一个启动脚本
access-service-start.sh
#!/bin/sh #java默认放在/usr/local/soft/jdk1.8.0_251 JAVA_HOME=/usr/local/soft/jdk1.8.0_251 PATH=$JAVA_HOME/bin:$PATH export JAVA_HOME PATH # 设置jar的启动位置,并且将日志打印到jar的根目录中的access.log文件中 nohup java -jar /usr/local/project/access/access.jar >/usr/local/project/access/access.log 2>&1 & #获取PID echo $! > /usr/local/project/access/access.pid
-
新建一个停止脚步
access-service-stop.sh
#!/bin/sh PID=$(cat /usr/local/project/access/access.pid) kill -9 $PID
在/usr/lib/systemd/system
目录新建系统启动或开机启动脚步access.service
#!/bin/sh
[Unit]
Description=access
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
#核心部分
ExecStart=/usr/local/project/access/lib/access-service-start.sh
ExecStop=/usr/local/project/access/lib/access-service-stop.sh
PrivateTmp=true
[Install]
WantedBy=multi-user.target
二、给启动、停止脚本设置权限
chmod +x /usr/local/project/access/access-service-start.sh
chmod +x /usr/local/project/access/access-service-stop.sh
三、设置完成,常用命令:
systemctl start access //启动jar
systemctl status access //查看运行状态
systemctl stop access //停止jar运行
systemctl enable access //设置为开机自启动
systemctl disable access //禁止开机启动
tail -f access.log //动态查询日志文件