Cronjobs are quick and efficient for mostly backend automation or lightweight notifications, but are rather tedious in situations where embedded applications have permissions restrictions or behave weirdly without a full userspace or env import.
systemd user services (with timers) seem to be a nice alternative (for systemd-based Linux distros.)
- systemd user files run as the user and don't require sudo or root to setup or run.
- Timer script
- Service script
- If
~/.config/systemd/user/
doesn't exist, create it:
mkdir -p ~/.config/systemd/user/
- Create the service file:
myscript.service
in~/.config/systemd/user/
:
[Unit]
Description=Conscise description of this service
ConditionPathExists=/your/path/to/script
[Service]
ExecStart=/your/path/to/script
[Install]
WantedBy=default.target
- Create the timer file (schedule) for
myscript
asmyscript.timer
:
[Unit]
Description=Concise description of this scheduler
[Timer]
OnBootSec=2min
OnUnitActiveSec=5m
[Install]
WantedBy=timers.target
- **OnBootSec** = how soon to start the service after boot
- **OnUnitActiveSec** = frequency threshold to re-launch `myscript`
- Confirm your directory looks like so:
~/.config/systemd/user/:
├── myscript.service
└── myscript.timer
-
Activate the service (script) on every startup:
One and done command; this won't need to be run again after the timer is active.
systemctl --user enable myscript
-
Set the timer for startup during boot:
One and done command; this won't need to be run again after the timer is active.
systemctl --user enable myscript.timer
-
Set the timer for startup during boot:
One and done command; this won't need to be run again after the timer is active.
systemctl --user enable myscript
-
Start the service
One and done command; this won't need to be run again after the timer is active.
systemctl --user start myscript --now
-
Start the timer:
One and done command; this won't need to be run again after the timer is active.
systemctl --user start myscript.timer --now
- Modifying the script doesn't appear to require a restart
- Modifying the service or timer settings will require a simple restart of a daemon:
systemctl --user daemon-reload
systemd user services have the same tools available as system-level services.
-
See when the timer is scheduled to run next:
systemctl --user status myscript.timer
-
Check if everything is going smoothly with the service
systemctl --user status myscript
(note that inactive/dead isn't what you may think; it simply means the service did it's job and went dormant. The timer will come by and activate it when it's scheduled to.)
-
Restart the timer:
systemctl --user restart myscript.timer
-
Restart the service:
systemctl --user restart myscript
-
Stop the timer:
systemctl --user stop myscript.timer
- Stop the script:
systemctl --user stop myscript
The Arch Linux systemd/user wiki has much greater detail for additional options and features.