You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置Linux systemd守护服务实现故障自动重启与开机自启

配置调整及操作步骤

一、修改stargate.service配置

1. 新增异常终止自动重启规则

在[Service]段落添加如下两行配置:

  • Restart=on-failure:仅在服务异常退出(非0退出码、被信号杀死、启动/停止超时等场景)时自动重启,手动执行stop命令不会触发重启,符合常规运维需求
  • RestartSec=5:设置重启前的等待间隔为5秒,避免服务故障时频繁重启占用系统资源,间隔时长可根据实际需求调整

你现有配置的[Install]段已经设置了WantedBy=default.target,无需额外修改即可支持开机自启能力。

调整后的完整配置示例

[Unit]
Description=stargate

[Service]
Type=simple
PIDFile=/app/stargate/stargate.pid
ExecStart=/app/stargate/stargate.sh start
ExecReload=/app/stargate/stargate.sh restart
ExecStop=/app/stargate/stargate.sh stop
# 异常自动重启配置
Restart=on-failure
RestartSec=5

[Install]
Alias=stargate
WantedBy=default.target

二、执行生效操作

修改配置文件后依次执行以下命令:

  • 重新加载systemd守护进程配置:systemctl daemon-reload
  • 重启服务让新配置生效:systemctl restart stargate.service
  • 启用开机自启:systemctl enable stargate.service

验证方式

  • 验证异常重启:找到stargate服务的进程ID,执行kill <进程ID>,等待几秒后执行systemctl status stargate.service查看服务是否恢复运行
  • 验证开机自启:重启服务器后执行systemctl is-active stargate.service,返回active即为正常启动

内容的提问来源于stack exchange,提问作者David

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 05:39:05