Create cronjob alternative on Ubuntu Core
The task
I need to run a shell script every hour.
Limitations
- Ubuntu Core as a read-only filesystem, only user home folder is writable.
The basic concepts
Cronjobs can’t be used on a read-only fs. Only snap packages can be installed on Ubuntu core. It is possible to make a snap for this. It is possible to create a systemd user timer.
The solution
I choose the more agnostic and simple way: systemd user timer!
First we need to create systemd user folder
$ mkdir -p ~/.config/systemd/user/
Systemd user timers only run if the user is logged in, to avoid this we need to enable lingering session with:
$ sudo loginctl enable-linger <username>
Now create servicetimer unit file (update-ddns.service)
[Unit]
Description="A script to update DDNS record value"
Wants=update-ddns.timer
[Service]
Type=oneshot
ExecStart=/home/<username>/update-ddns.sh
[Install]
WantedBy=multi-user.target
And timer unit file (update-ddns.timer)
[Unit]
Description="Update DDNS record value"
Requires=update-ddns.service
[Timer]
Unit=update-ddns.service
OnCalendar=hourly
[Install]
WantedBy=multi-user.target
And scp them to ~/.config/systemd/user folder.
And then finish systemd setup:
$ systemctl --user daemon-reload
and then you can enable the timer with
$ systemctl --user enable update-ddns.timer --now
and view the result with
$ systemctl --user list-timers
or
$ systemctl --user status update-ddns
Don’t forget the /<username>/update-ddns.sh
script!
Done!
Relax and grab a coffee…
Photo credit: Danilo Paissan on Visualhunt.com