Schedule tasks in systemd
Given that cron
is not installed by default on a Fedora Silverblue host, it can either be added as a layered package or you can use systemd
which is available by default.
As I want to keep layered packages to bare minimum, I have chose to utilise systemd
to configure any scheduled tasks I may need.
Systemd requires minimum of 2 files to schedule task:
- a service unit
- and a timer unit
- and optionally a script if command to run is not so simple
Files should be placed in ~/.config/systemd/user
directory, which may not exist yet. If need be, create it with:
mkdir -p ~/.config/systemd/user
As an example - I wanted to have the Traefik container that I setup previously as the proxy for my local development environment, to be started automatically.
# schedule-traefik.service
[Unit]
Description=Run script to start Traefik container
[Service]
Type=simple
ExecStart=%h/bin/schedule-traefik.sh
RemainAfterExit=true
[Install]
WantedBy=default.target
# schedule-traefik.timer
[Unit]
Description=Schedule timer to start Traefik container
[Timer]
#Run 60 seconds after boot for the first time
OnBootSec=60
[Install]
WantedBy=timers.target
# bin/schedule-traefik.sh
#!/bin/sh
cd $HOME/development/podman-traefik-dev-box/ && $HOME/.local/bin/ansible-playbook play.yaml
Once the .service and .timer unit files are in place, the service can be enabled with:
systemctl --user enable schedule-traefik.service
and manually tested with:
systemctl --user start schedule-traefik.service
Also check the output and that the container is running:
journalctl --user -fxeu schedule-traefik.service
podman ps -a
Reboot and check again. Container should be up and running as expected.