this post was submitted on 05 Feb 2025
10 points (100.0% liked)

Linux Questions

1591 readers
7 users here now

Linux questions Rules (in addition of the Lemmy.zip rules)

Tips for giving and receiving help

Any rule violations will result in disciplinary actions

founded 2 years ago
MODERATORS
 

Hello,

I have been trying to create a system service that would run a script on shutdown (hence why I went for a system service over a user service) and landed on something like this

[Unit]
Description=Run backup script on shutdown
DefaultDependencies=no
Before=poweroff.target halt.target
Requires=network.target

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/var/home/blackeco/scripts/backup.sh
User=blackeco
Group=blackeco

[Install]
WantedBy=poweroff.target halt.target

Unfortunately, when the shutdown occurs, systemd fails to execute the script:

backup-on-shutdown.service: Unable to locate executable '/var/home/blackeco/scripts/backup.sh': Permission denied
backup-on-shutdown.service: Failed at step EXEC spawning /var/home/blackeco/scripts/backup.sh: Permission denied

This script is correctly owned by user blackeco and permissions look fine

$ ls -la /var/home/blackeco/scripts
drwxr-xr-x. 1 blackeco blackeco 154  5 Feb. 13:50 ./
drwxr-xr-x. 1 blackeco blackeco 116  3 Feb. 13:07 ../
-rwxr-xr-x. 1 blackeco blackeco 794  4 Feb. 15:44 backup.sh*

I'm very puzzled as to why. I'm running Bluefin 41 (itself based on Fedora Silverblue 41).

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 2 points 2 months ago (1 children)

As I said, this is a system service

[–] [email protected] 4 points 2 months ago (1 children)

This seems to be a systemd feature, system services can't touch home directories by default.

https://unix.stackexchange.com/a/684074

I think a user script would still work. Or you could set the flag that would let system services access your home.

[–] [email protected] 3 points 2 months ago

I would try ProtectHome=read-only but then restic wouldn't be able to write its local cache to ~/.restic.

I went for a user service first to make my life easier, but unfortunately you can't use targets poweroff.target and halt.target

Unit /etc/systemd/user/backup-on-shutdown.service is added as a dependency to a non-existent unit poweroff.target
Unit /etc/systemd/user/backup-on-shutdown.service is added as a dependency to a non-existent unit halt.target.

I may be in a bind then...