How (and when) to use systemd timer instead of cronjob
Introduction
* * * * * bash demo.sh
Just a single line of code is sufficient to schedule a cron job. However, there are some scenarios where I find systemd timer more useful than cronjob.
How to use systemd timer
We need to create a service file(contains the script to be run) and a timer(contains the schedule).
# demo.service [Unit] Description=Demo service [Service] ExecStart=bash demo.sh
# demo.timer [Unit] Description=Run myscript.service every 1 minutes [Timer] OnBootSec=1min OnUnitActiveSec=1min [Install] WantedBy=multi-user.target
We can copy these files to /etc/systemd/system/
and enable the timer.
$ sudo cp demo.service demo.timer /etc/systemd/system/
$ sudo systemctl daemon-reload
$ sudo systemctl enable --now demo.timer
We can use systemctl
to see when the task is executed last and when it will be executed next.
$ sudo systemctl list-timers --all
Use Cases
-
Singleton - In the above example, lets say
demo.sh
takes ~10 minutes to run. With cron job, in ten minutes we will have 10 instances ofdemo.sh
running. This is not ideal. With systemd timer, it will ensure only one instance ofdemo.sh
is running at a time. -
On demand runs - If we want to test out the script/job, systemd allows us to immediately run it with usual
systemctl start demo
without needing to run the script manually. -
Timer - With cron, we can run tasks upto a minute precision. Timer can run tasks till
second
level precision.
[Timer] OnCalendar=*-*-* 15:30:15
In addition to that, we can run tasks based on system events. For example, we can run a script 15 minutes from reboot.
[Timer] OnBootSec=15min
Conclusion
Systemd timer is a powerful tool that can replace cronjob in many scenarios. It provides more control and flexibility over cronjob. However, cronjob is still a good choice for simple scheduling tasks.
Need further help with this? Feel free to send a message.