linux自定义服务
1.在/lib/systemd/system下新建一个文件,比如test.service,内容如下
[Unit]
Description=test.service #服务描述
Documentation=no #文档描述
[Service]
WorkingDirectory=/opt/test/ #工作目录
Type=forking #守护线程启动
ExecStart=/opt/test/test.sh > /dev/null 2>&1 #启动脚本
ExecReload=/bin/kill -HUP $MAINPID # 重启,mainpid指向服务运行时的主进程号
KillMode=process # 关闭,杀死进程
Restart=on-failure #服务异常退出会重新启动
RestartSec=1s #重启等待时间
[Install]
WantedBy=multi-user.target graphic.target #表示多用户命令行状态
2.启动脚本test.sh
#!/usr/bin/env bash
rm -f /opt/test/tpid
export PATH=/usr/local/jdk/jdk1.8.0_91/bin:$PATH
nohup java -Xms4096m -Xmx4096m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./logs/heapdump-test.hprof -jar ./test.jar server ./config/application.yml >/dev/null 2>&1 &
echo $! > /opt/test/tpid
3.服务启动
systemctl start test.service #服务注册启动
systemctl start test #启动
systemctl stop test #停止
mv /lib/systemd/system/test.service lib/systemd/system/test.service_bak 卸载