Centos7利用systemctl添加自定義系統服務


1.創建啟動服務的shell腳本,如下start.sh

#!/bin/sh
export JAVA_HOME=/usr/local/java/jdk1.8.0_91
export PATH=$JAVA_HOME/bin:$PATH

cd /dwgj/services/gps-consume-service
java -jar ./lib/gps-consume-service.jar &
echo $! > /var/run/gps-consume-service.pid

2.創建停止服務的shell腳本,如下stop.sh

#!/bin/sh
PID=$(cat /var/run/gps-consume-service.pid)
kill -9 $PID

3.進入/usr/lib/systemd/system或者/etc/systemd/system下,創建服務的systemctl腳本,如下gps-consume.service

[Unit]
Description=the service description
After=network.target

[Service]
Type=forking
ExecStart=/dwgj/services/gps-consume-service/start.sh
ExecStop=/dwgj/services/gps-consume-service/stop.sh

[Install]
WantedBy=multi-user.target

4.systemctl常用命令systemctl status gps-consume 查看服務狀態

 

systemctl start gps-consume       啟動服務
systemctl stop gps-consume        停止服務
systemctl restart gps-consume    重啟服務
systemctl enable gps-consume    設置開機自啟
systemctl kill gps-consume       殺死服務
systemctl list-units --type=service    查找所有服務

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM