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

Linux下实现关机前执行自定义服务的问题与解决

实现关机前自动运行的systemd服务及问题排查

操作步骤

  • 创建自定义target文件 /etc/systemd/system/custom.target,内容如下:
[Unit]
Description=Custom Target
Requires=multi-user.target
After=multi-user.target
AllowIsolate=yes
  • 在 /etc/systemd/system/custom.target.wants/ 目录下创建 app-shutdown.service 单元文件,内容如下:
[Unit]
Description=App Shutdown
After=multi-user.target

[Service]
Type=oneshot
ExecStart=echo 'started'
ExecStop=bash /myhome/app-shutdown.sh
RemainAfterExit=yes
TimeoutStopSec=480

[Install]
WantedBy=custom.target

遇到的问题

  • 执行 systemctl list-units --type target --all 时,无法找到 custom.target
  • 执行 systemctl isolate custom.target 和 ln -sf /etc/systemd/system/custom.target /etc/systemd/system/default.target 后,custom.target 已加载激活,但系统重启后,执行 systemctl status app-shutdown 显示服务处于 inactive (dead) 状态

排查与解决

  • 经排查发现之前的 ln 命令未成功执行,重新执行该命令
  • 依次执行以下命令:
    sudo systemctl daemon-reload
    sudo systemctl enable app-shutdown
    sudo systemctl start app-shutdown
    
  • 再次重启系统后,服务恢复正常运行

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 20:54:59