Obsidian Sync as service

Posted by Munish Mehta on Sun, Nov 6, 2022

This post is third and final part of the series where i am demostrating to setup automated remote state management of our notes. Before continuing here, i would recommend to go through part-1 and part-2 of the sereis.

Service and Timer

Create a systemd service and a timer . The function of timer unit is to trigger service unit and the service unit will execute the obsidian-sync script

Timer Unit

Create a timer obsidian-sync.timer unit

 1bash -c "cat >$HOME/.config/systemd/user/obsidian-push.timer" <<EOF
 2# Timer for obsidian-push.service
 3# By Munish Mehta
 4
 5[Unit]
 6Description=Starts the obsidian-push.service
 7Requires=obsidian-push.service
 8
 9[Timer]
10Unit=obsidian-push.service
11OnCalendar=MON-FRI *-*-* 17:00:00
12
13[Install]
14WantedBy=timers.target
15EOF

Test the status of obsidian-push.timer timer

1systemctl --user status obsidian-push.timer

Output

1obsidian-push.timer - Starts the obsidian-push.service
2    Loaded: loaded (/home/munmeh/.config/systemd/user/obsidian-push.timer; disabled; vendor preset: enabled)
3    Active: inactive (dead)
4     Trigger: n/a
5    Triggers: ● obsidian-push.service

Service Unit

Create a service which executes the [[obsidian-sync]] bash script to push changes to origin.

Update your obsidian valut path in line below

 1export OBSIDIAN_LOCAL_PATH="$HOME/Documents/gitrepo/obsidian-personal-vault/"
 2bash -c "cat >$HOME/.config/systemd/user/obsidian-push.service" <<EOF
 3#This service unit is for executing obsidian-sync.sh script which in turn push local obsidian changes to git remote
 4#By Munish Mehta
 5[Unit]
 6Description=Push obsidian local vault changes to git remote https://github.com/mumehta/obsidian-personal-vault
 7Wants=obsidian-push.timer
 8
 9[Service]
10Type=oneshot
11ExecStart=/bin/bash -c "$HOME/.obsidian/obsidian-sync.sh $OBSIDIAN_LOCAL_PATH"
12
13[Install]
14WantedBy=default.target
15EOF

Test status of obsidian-push service

1systemctl --user status obsidian-push.service

Output

1● obsidian-push.service - Push obsidian local vault changes to git remote https://github.com/mumehta/obsidian-personal-vault
2        Loaded: loaded (/home/munmeh/.config/systemd/user/obsidian-push.service; disabled; vendor preset: enabled)
3        Active: inactive (dead)

Start service and the timer

1systemctl --user start obsidian-push.timer
2systemctl --user start obsidian-push.service

Check status

service
1systemctl --user status obsidian-push.service

Output

1obsidian-push.service - Push obsidian local vault changes to git remote https://github.com/mumehta/obsidian-personal-vault
2    Loaded: loaded (/home/munmeh/.config/systemd/user/obsidian-push.service; disabled; vendor preset: enabled)
3     Active: inactive (dead) since Thu 2022-09-08 15:22:10 AEST; 1min 27s ago
4     TriggeredBy: ● obsidian-push.timer
5    Process: 2873361 ExecStart=/bin/bash -c /home/munmeh/.obsidian/obsidian-sync.sh /home/munmeh/Documents/gitrepo/obsidian-personal-vault/ (code=exi>
6   Main PID: 2873361 (code=exited, status=0/SUCCESS)
timer
1systemctl --user status obsidian-push.timer

Output

1obsidian-push.timer - Starts the obsidian-push.service
2	 Loaded: loaded (/home/munmeh/.config/systemd/user/obsidian-push.timer; disabled; vendor preset: enabled)
3	 Active: active (waiting) since Thu 2022-09-08 15:21:55 AEST; 3min 27s ago
4	Trigger: Thu 2022-09-08 17:00:00 AEST; 1h 34min left
5   Triggers: ● obsidian-push.service
6    Sep 08 15:21:55 mmsys systemd[2997]: Started Starts the obsidian-push.service.

Enable timer and service units

1systemctl --user enable obsidian-push.timer
2systemctl --user enable obsidian-push.service

Check logs

1journalctl --user -S today -u obsidian-push.timer
2journalctl --user -S today -u obsidian-push.service

If you like post, consider buy me a coffee! ☕

Reference



comments powered by Disqus