Automatically storing my notes
I tried out obsidian a while back, and while I did not particularly like the experience as a whole, one thing I did like was the git integration. Back in my Emacs I have to manually call up magit to do the commits. So I decided to solve it in “The Linux way ™”; a small program that does the trick.
Normally I would use crontab for this job, but modern Linux systems come equipped with something more powerful. systemd is on pretty much every linux system now, if you like it or not. It provides a framework of managing services and tasks.
The script
First we need a script that does what we need it to do. I put it in my ~/.local/bin directory as store-notebook.sh. All it does is to go into my notes directory, does a git add with a commit, and only if there are commits, a pull and a push.
#!/usr/bin/env sh
NOTEBOOK_PATH=~/Nextcloud/obsidian
cd $NOTEBOOK_PATH &&
git add . &&
git commit -am "Auto commit from laptop" &&
git pull -r origin main &&
git push origin main
The service
Then we want a service to run this script. User files are stored in your user systemd directory. Mine is: /home/arjen/.config/systemd/user/.
I created the file store-notebook.service into that directory.
[Unit]
Description=A job to push notebook changes to git
[Service]
Type=simple
ExecStart=/home/arjen/.local/bin/store-notebook.sh
[Install]
WantedBy=default.target
The timer
Just having the service does nothing, as it is not run. You can run it manually, but that defeats the purpose. The next piece is to configure the time, store-notebook.timer. This is basically the old-skool cron job.
[Unit]
Description=Store notebook every 10 minutes
RefuseManualStart=no # Allow manual starts
RefuseManualStop=no # Allow manual stops
[Timer]
#Execute job if it missed a run due to machine being off
Persistent=true
#Run 120 seconds after boot for the first time
OnBootSec=120
#Run every 10 minute thereafter
OnUnitActiveSec=600
#File describing job to execute
Unit=store-notebook.service
[Install]
WantedBy=timers.target
There are some comments in the file that allow you to configure timing of the task.
Then, all that is left is to enable everything.
chmod +x store-notebook.sh
systemctl --user enable store-notebook.service
systemctl --user start store-notebook.service
systemctl --user enable store-notebook.timer
systemctl --user start store-notebook.timer
Checking all the things
You can verify everything is running by calling the status on the service/timer or listing the timers.
systemctl --user status store-notebook.service
systemctl --user status store-notebook.timer
systemctl --user list-timers --all
Tags: #automation #orgmode #emacs