Centos7将jar包设置为开机自启

  • 编写jar包的可执行启动文件

首先新建一个文件

vim run.sh

内容如下 

#!/bin/sh
nohup /usr/local/jdk1.8/bin/java -jar -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m -Xms1024m -Xmx1024m -Xmn256m -Xss256k -XX:SurvivorRatio=8 -XX:+UseConcMarkSweepGC /home/zhtel/tydic/tydic-admin.jar >/home/zhtel/logs/tydic.out 2>&1 &

给文件加上可执行权限

chmod a+x run.sh
  • 将run.sh添加到系统服务

 进入到目录

 /etc/systemd/system/

 新建文件xxx.service,内容如下:

#!/bin/sh

[Unit]
Description = tydic-admin
After=syslog.target network.target remote-fs.target nss-lookup.target network.target network-online.target dbus.service


[Service]
Type = forking
ExecStart = /home/zhtel/tydic/run.sh
PrivateTmp=true
Restart=always
RestartSec=1

[Install]
WantedBy = multi-user.target

 给文件赋权754,chmod 754 xxx.service 

  • 将服务添加到开机自启

systemctl daemon-reload #重载系统服务
systemctl start xxx.service #启动服务
systemctl enable xxx.service #将服务设置为开机自启
systemctl disable xxx.service #将服务取消开机自弃
systemctl status xxx.service #查看服务状态

reboot重启服务器后查看服务运行状态,结果如下:

[root@localhost system]# systemctl status tydic-admin.service
● tydic-admin.service - tydic-admin
   Loaded: loaded (/etc/systemd/system/tydic-admin.service; enabled; vendor preset: disabled)
   Active: active (running) since 一 2019-12-23 16:44:42 CST; 11min ago
  Process: 984 ExecStart=/home/zhtel/tydic/run.sh (code=exited, status=0/SUCCESS)
 Main PID: 1546 (java)
   CGroup: /system.slice/tydic-admin.service
           └─1546 /usr/local/jdk1.8/bin/java -jar -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m -Xms1024m -Xmx1024m -Xmn256m -Xss256k -XX:SurvivorRatio=8 -XX:+UseConcMarkSweepGC /home/zhtel/ty...

12月 23 16:44:42 localhost.localdomain systemd[1]: Starting tydic-admin...
12月 23 16:44:42 localhost.localdomain systemd[1]: Started tydic-admin.
[root@localhost system]# 

 

你可能感兴趣的:(linux,linux,centos)