自定义 systemd 服务不会自动启动

自定义 systemd 服务不会自动启动

我编写了一个脚本和一个服务,用于在关机时关闭我的外置 Seagate 硬盘。第一次使用时它起作用了,但当我重新启动计算机时它却无法激活。我总是必须在关机前手动启动它,这违背了初衷。也许有人可以帮助我让服务以这样的方式工作:当我关闭计算机时它也会关闭硬盘。

脚本:

#!/bin/sh

disk_uuid="MY-UUID-PLACEHOLDER"

udisksctl unmount -b /dev/disk/by-uuid/${disk_uuid} 
udisksctl power-off -b /dev/disk/by-uuid/${disk_uuid}

exit 0

服务文件:

[Unit] 
Description=Shut down external disks
DefaultDependencies=no
Before=shutdown.target reboot.target kexec.target halt.target

[Service] 
Type=oneshot ExecStart=/usr/sbin/power-off-disk.sh
RemainAfterExit=yes

[Install] 
WantedBy=halt.target kexec.target reboot.target shutdown.target

来自 journalctl -u 的日志:

Feb 22 11:53:50 ace-desktop systemd[1]: Starting Shut down external disks...
Feb 22 11:53:50 ace-desktop power-off-disk.sh[9810]: Error connecting to the udisks daemon: GDBus.Error:org.freedesktop.DBus.Error.NoReply: Message recipient disconnected from message bus without replying
Feb 22 11:53:50 ace-desktop power-off-disk.sh[9880]: Error connecting to the udisks daemon: Error calling StartServiceByName for org.freedesktop.UDisks2: Transaction for udisks2.service/start is destructive (dev-sdb1.swap has 'stop' job queued, but 'start' is included in transaction).
Feb 22 11:53:50 ace-desktop systemd[1]: power-off-disk.service: Deactivated successfully.
Feb 22 11:53:50 ace-desktop systemd[1]: Finished Shut down external disks.

所以我现在编辑了我的服务文件:

[Unit]
Description=Shut down external disks
DefaultDependencies=no
Before=udisks2.service shutdown.target reboot.target halt.target

[Service]
Type=oneshot
ExecStart=/bin/true
ExecStop=/usr/sbin/power-off-disk.sh
RemainAfterExit=no

[Install]
WantedBy=shutdown.target reboot.target halt.target

但它并没有改变什么。

systemctl 启动磁盘关闭电源

手动激活时有效,但关机时无效。非常感激帮助。

相关内容