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 查找所有服務
