Use systemd timers as a replacement for cron: schedule services the same way you created them.
Create the timer
- You need to create a
/etc/systemd/system/with the same name as the service you want to schedule.
- This is the basic content of a
[Unit] Description=timer description [Timer] OnBootSec=15min [Install] WantedBy=timers.target
Description: timer description.
OnBootSec: Defines a timer relative to when the computer was booted up. If you only use a number, means “seconds”. These are some examples of time formats:
- 5h 30min
OnActiveSec: Defines a timer relative to the moment the timer unit is activated.
OnCalendar: This option use system clock and not an event to define when the service should start. Uses the following format:
DayOfWeek Year-Month-Day Hour:Minute:Second
# Run every day at 5 p.m. OnCalendar=*-*-* 17:00:00 # Run first day of the month only if that day is Monday OnCalendar=Mon *-*-01 12:00:00
# You can use shortcuts like: OnCalendar=hourly OnCalendar=weekly
Unit: You can define the unit you want to control if it has a different name than the timer.
WantedBy: intructions for the timer installation (
systemctl enable). Typing
timers.targetwould be fine for most cases. Services don’t need an
[Install]section if they have a timer.
Start / Enable the timer
- Reload systemd with
sudo systemctl daemon-reload.
- Like any other unit, you can type
sudo systemctl start myservice.timer(replacing
myservicewith your timer name) to start the timer and
sudo systemctl enable myservice.timerto enable the timer.
If you have any suggestion, feel free to contact me via social media or email.
Latest tutorials and articles: