linux中jar添加到系统服务并设置开机自启

注:ContOS 7以上才适用

 

1.新增**.service配置文件 

vim /etc/systemd/system/test.service

2.写入配置

[Unit]
Description=test service
[Service]
WorkingDirectory=/usr/local/project/
PrivateTmp=true
Restart=always
Type=simple
ExecStart=/usr/java/jdk1.8.0_231-amd64/bin/java -Xmx1024m -Xms512m -Xss256k -jar /usr/local/project/test.jar
ExecStop=/usr/bin/kill -15 $MAINPID

[Install]
WantedBy=multi-user.target

########配置参数描述如下: ########################################################

[Unit]
Description=test service       #要设置的服务名:test
[Service]
WorkingDirectory=/usr/local/project/      #jar存放的位置 /usr/local/project/
PrivateTmp=true
Restart=always
Type=simple
ExecStart=/usr/java/jdk1.8.0_231-amd64/bin/java -Xmx1024m -Xms512m -Xss256k -jar /usr/local/project/test.jar   #java绝对路径 项目的绝对路径
ExecStop=/usr/bin/kill -15 $MAINPID

[Install]
WantedBy=multi-user.target

###################################################################################

3.重新载入systemd

systemctl daemon-reload

4.查看服务状态

systemctl status test             #test服务名

5.其他常用命令

systemctl start test         #启动服务
systemctl stop test            #停止服务
systemctl reload test           #重新加载

6.设置开机自启

systemctl enable test

7.取消开机自启

systemctl disable test

你可能感兴趣的:(笔记)