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