I keep a daily journal in an Emacs org-mode file. Sometimes due to clumsy keypresses in Emacs, I have temporarily deleted (!) lines or sections (!). I think I have always caught these mistakes, but to be safe, I decided to create a git repository for it and do a daily commit.
In the past, I'd have used a cron job, but in recent years I've tried to make more and more use of systemd and timers. Is one better than the other? I'm going to ignore that question and proceed. :D
For this task, which I'm calling "log_git_committer" (very uninspired), I have four files:
- log_git_committer.sh - a Bash script that handles actually committing changes
- log_git_committer.service - a systemd service file that references the .sh file
- log_git_committer.timer - a systemd timer file that implicitly references .service unit and describes the interval
- Makefile - a simple make file to install and uninstall my files as I work on it
Below are some of the more interesting notes from working on it, and the systemd files.
Notes
Some notable pieces that came up:
- .service:
- Type=oneshot: since I'm running this from a timer, I just want this service unit to be a "oneshot", as it's not actually a service that would keep running in the background.
- ExecStart= and %h: I'm installing the shell script locall in my ~/.local/bin directory. ExecStart in a .service won't accept ~/ or $HOME/ in a path, preferring absolute paths. However, systemd has some specifiers like %h that will substitute in a user's path when a service unit is run with --user.
- .timer:
- OnCalendar=: it has a lot of options that are similar to cron. Neat.
- retroactively running missed timers? Yes, if I set a timer to run at 3AM but I put my computer to sleep at 1AM and turn it back on at 8AM, the timer will then execute. If I end up in Narnia and my computer is asleep for 3 days while I'm away (a lifetime there), it will then run the service just once to catch up. Nice.
- Makefile:
- unit file syntax checking: systemd provides a command that lets you analyze your unit files to check for correctness. E.g.systemd-analyze verify log_git_committer.timerso I added that to a target.
systemd-analyze verify log_git_committer.service - systemd install directory: for non-root user services and timers, the install directory is ~/.config/systemd/user/
Some other locations on my Fedora 38 system for systemd unit files include: - /usr/lib/systemd/user/
- /usr/lib/systemd/service/
- /etc/systemd/user/
- /etc/systemd/system/
- systemd and reloading updated .service files: if I make a change to a .service file, systemd would like me to reload the new one from disk. So, in my Makefile, after I copy .service and .timer files into their install directory, I call:systemctl --user daemon-reload
log_git_committer.sh
If there's interest, I can share this, but it's pretty simple and straight forward.
log_git_committer.service
[Unit]
Description=Commit changes to a log file
[Service]
Type=oneshot
ExecStart=%h/.local/bin/log_git_committer.sh
[Install]
WantedBy=multi-user.target
log_git_committer.timer
[Unit]
Description=Do a daily commit of the log file.
[Timer]
OnCalendar=3:00:00
Persistent=true
[Install]
WantedBy=timers.target
Resources
- https://www.freedesktop.org/software/systemd/man/systemd.service.html
- https://wiki.archlinux.org/title/systemd
- https://wiki.archlinux.org/title/systemd/Timers
- https://wiki.archlinux.org/title/Systemd/User#Basic_setup
- https://documentation.suse.com/smart/systems-management/html/systemd-working-with-timers/index.html
Keine Kommentare:
Kommentar veröffentlichen