I would like to use systemd
to accomplish this if possible. This is what I have done so far.
- Wrote a script in
fish
that will stage, commit and push files to a repository. Script made executable withchmod u+x <script>.fish
. - Wrote a service unit.
- Reloaded with
systemctl --user daemon-reload
, enabled withsystemctl --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.