Love it or hate it, systemd is here to stay.  It seems more and more Linux distributions are putting systemd as their default init system. Although initially systemd was meant to replace the existing init system, but systemd dev decided to add features beyond a typical init system. One of it is task/job scheduler which traditionally handled from cron system.

While working on the online quiz system, I came to a need to run a script every 10 minutes. Of course I can easily add an entry to the cron but I thought this might be a good opportunity to try scheduling this task with systemd. So here’s how you can use systemd instead of cron system to run a job.

First, I created a service file (a file with .service extension). This is actually to define the script that I want to run. In my example the “Type” under [Service] directive is set to “simple” since the script I wanted to run was simply a shell script. The “ExecStart” option specifies the command to be executed. For other options, you may refer to the official documentation.

Description=Add the description of your service here


There is nothing special with the service file. It looks like a typical service file, only much simpler.

In systemd, there is a scheduling facility called timer. So next, I created a timer file (a file with .timer extension) with the following entries:

Description=Description for my timer directive

# Runs every 10 minutes
OnCalendar=*-*-* *:00,10,20,30,40,50:00
# Run even we site the time set
Unit=<the service file I created above>


The “OnCalendar” option does look similar to cron but systemd support various other formats. The “Persistent” option means systemd will keep track when was the last time the event triggered and is useful in case the timer misses any run and systemd will ensure that my script will be executed at least once during the time the timer is inactive (for example during downtime). For other timer options you may refer to the documentation.

After having those 2 files, I just copied to /etc/systemd/system and execute the systemctl command to run the timer and enable it to persist after reboot

$ systemctl start my_timer_file
$ systemctl enable my_timer_file

Now I have systemd completely replaced crond for my simple need.