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.

muru
  • 69,900
  • 13
  • 192
  • 292
YJH16120
  • 113
  • 5
  • Related: [How to write a Systemd unit that will fire before networking goes down](https://unix.stackexchange.com/questions/294047) – Freddy Feb 07 '23 at 12:02
  • I did not consider the network to be shut down. Thanks @Freddy. I'll look into the link. – YJH16120 Feb 07 '23 at 12:05
  • @Freddy I took a look at the linked answer and I added my comment in a quote block. Basically it didn't work. – YJH16120 Feb 07 '23 at 12:37

1 Answers1

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.

Freddy
  • 25,172
  • 1
  • 21
  • 60
  • 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 08 '23 at 00: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 08 '23 at 07:11
  • Ohh, I see. Thanks so much. – YJH16120 Feb 08 '23 at 10:11