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

Linux Questions

1374 readers
1 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
[–] bjoern_tantau@swg-empire.de 2 points 2 weeks ago (5 children)

I'm not familiar with Silverblue but home being in /var is sus. Usually it's in /home. But maybe it's mounted in a weird Silverblue way and gets unmounted before it runs.

But running scripts on shutdown is hard to impossible. I always wanted to run automatic updates on shutdown but they don't have networking even if the unit file requires networking. I haven't seen anyone properly manage to do that yet, so good luck. And please make a post if it does end up working. Then I will revisit my own efforts again.

[–] whatsgoingdom@rollenspiel.forum 2 points 2 weeks ago (2 children)

If that's the reason maybe OP can add the shutdown as the last step on the script and execute the script instead of the shut down button as a work around.

[–] BlackEco@lemmy.blackeco.com 1 points 2 weeks ago (1 children)

No, I really don't want to hijack the UI for this, as it could break with a DE update. And that wouldn't work when shutting down from the console.

[–] whatsgoingdom@rollenspiel.forum 1 points 2 weeks ago* (last edited 2 weeks ago)

Doesn't have to be, e.g. I have a stream deck and mapped a script to one of the buttons. Or put it as an executable file on your desktop or wherever and use it instead of the normal shutdown button.

load more comments (2 replies)