添加守护进程
vim /etc/systemd/system/xxx.service
[Unit]
Description=Go Socket Service
After=network.target[Service]
Type=simple
ExecStart=/data/quwan/quwan_ws
WorkingDirectory=/data/quwan
# 停止前发送信号
ExecStop=/bin/kill -SIGTERM $MAINPID
# 如果超过20s 进程还没停止,则强制重启
TimeoutStopSec=20s
Restart=always
# 重启在1s后重启
RestartSec=1s
# 标准输出到固定文件
StandardOutput=append:/data/quwan/output.log
#StandardOutput=file:/data/quwan/output.log
StandardError=inherit[Install]
WantedBy=multi-user.target
执行 sudo systemctl daemon-reload
使用脚本启动
#!/bin/shif [ "$1" = "status" ]; then# 只显示状态和版本信息./mq_quwan -vsystemctl status mq_baobao.service
else# 默认重启服务并显示状态./mq_quwan -vsystemctl restart mq_baobao.servicesystemctl status mq_baobao.service
fi#sudo systemctl daemon-reload
# sudo systemctl start baobao_go.service
# sudo systemctl enable baobao_go.service
# sudo systemctl status baobao_go.service