Score:1

Custom systemd service won't start automatically

ax flag

I wrote a script and a service to power down my external Seagate drive on shutdown. It worked the first time but when I restart my computer it won't activate. I always have to start it manually before shutdown, which defeats the purpose. Maybe someone here can help me making the service working in a way that always when I shutdown the computer it will also turn of the drive.

Script:

#!/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

Service file:

[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

Logs from 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.

So I now edited my Service file:

[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

It changes nothing though.

systemctl start power-off-disk

works, when activated manually, but not on shutdown. Help would really be appreciated.

Alfred avatar
ax flag
Hello, thank you for your answer. Yes, I enabled the service. I can also manually start it, which turns off the Drive but it won't start on shutdown, reboot or anything else.
Alfred avatar
ax flag
Would it maybe help if I replaced the "Before=" With "Before=umount.target" ?
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.