1

I would like to use systemd to accomplish this if possible. This is what I have done so far.

  1. Wrote a script in fish that will stage, commit and push files to a repository. Script made executable with chmod u+x <script>.fish.
  2. Wrote a service unit.
  3. Reloaded with systemctl --user daemon-reload, enabled with systemctl --user enable backup.service.

This is the fish script.

#!/usr/bin/fish

set note_dir /home/yjh/Documents/notes

cd $note_dir

set today (date "+(%A) %B-%d-%G %T")

git add . 
git commit -am $today
git push origin master

This is the service unit file.

[Unit]
Description=Backup obsidian notes before shutdown
DefaultDependencies=no
After=network-online.target
Wants=network-online.target
Before=halt.target reboot.target shutdown.target

[Service]
Type=oneshot
RemainAfterExit=true
ExecStop=fish /home/yjh/scripts/backup.fish

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

This is the service unit file after I applied the link to the answer provided by Freddy. That answer linked to another answer and I also applied those changes but it still didn't work.

I have ran the script in two ways manually with ./<script>fish and through starting it with systemctl like this systemctl --user start backup.service and they both are able to push my code to github. I have already setup SSH for that specific repository, so, no password or username is asked when I want to push to github.

3

1 Answer 1

1

TL;DR, try this simplified version:

[Unit]
Description=Backup obsidian notes before shutdown
After=network-online.target
Wants=network-online.target

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStop=fish /home/yjh/scripts/backup.fish

[Install]
WantedBy=default.target

Running this as a user service, my test script was not automatically started (inactive (dead) instead of active (exited)). At first I used WantedBy=multi-user.target in the install section. But this target is not available running user services, same as the halt.target or reboot.target. Switching it to WantedBy=default.target made the difference, see Systemd service does not start (WantedBy=multi-user.target).

Removing DefaultDependencies=no adds implicit dependencies Conflicts=shutdown.target and Before=shutdown.target, see systemd.target and systemd.special#shutdown.target.

Reenable the service after editing it to change the symlinks:

systemctl --user reenable backup.service

Then reboot and shutdown again to see if it works.

3
  • This helped, the only thing that was missing is an extra shutdown.target in WantedBy=. Could you update your answer before I accept it?
    – YJH16120
    Feb 8, 2023 at 0:33
  • AFAIK the install section is only responsible to enable the service and to start it and should be one the boot targets. I removed DefaultDependencies=no to add implicit dependencies to the shutdown.target, see my edit. You can view the dependencies with systemd-analyze --user dump.
    – Freddy
    Feb 8, 2023 at 7:11
  • Ohh, I see. Thanks so much.
    – YJH16120
    Feb 8, 2023 at 10:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .