本地有一个 data-service.jar
1. 编写启动脚本 data-service-start
[root@iz2ze0fq2isg8vphkpos5sz shell]# more data-service-start #!/bin/sh export JAVA_HOME=/usr/local/jdk1.8.0_131 export PATH=$JAVA_HOME/bin:$PATH java -jar /data/imgcloud/data-service.jar > /data/logs/data-service.log & echo $! > /var/run/data-service.pid
2 停止脚本
[root@iz2ze0fq2isg8vphkpos5sz shell]# more data-service-stop #!/bin/sh PID=$(cat /var/run/data-service.pid) kill -9 $PID
3. 在/usr/lib/systemd/system 下 编写 service 脚本
[root@iz2ze0fq2isg8vphkpos5sz shell]# cd /usr/lib/systemd/system [root@iz2ze0fq2isg8vphkpos5sz system]# more data-service.service [Unit] Description=data-service for mongodb After=syslog.target network.target remote-fs.target nss-lookup.target [Service] Type=forking ExecStart=/data/shell/data-service-start ExecStop=/data/shell/data-service-stop PrivateTmp=true [Install] WantedBy=multi-user.target
4. 相关命令
systemctl enable data-service 开机自启动
systemctl stop data-service 停止
system start data-service 启动
如果出现
service start operation timed out. Terminating
把.service的配置中
[Service]
#Type=forking 注释即可