Configuring Cron Jobs in Linux using Systemd Timers

On today’s tutorial, we’ll teach you how to use Systemd Timers to configure Cron Jobs in Linux. You may use timers instead of cron to schedule tasks while using systemd as the system and service manager. Timers are systemd unit files with the .timer suffix, and each one must have a matching unit file that defines the unit that will be triggered when the timer expires. A timer initiates a service with the same name as the timer, except for the suffix.

Understanding Cron | Systemd Timers

a.) Cron

Cron may plan a job to execute at many points of time, from minutes to months and beyond. It’s easy to set up, as it only requires one configuration file. Despite the fact that the configuration line is a little obscure. It may also be utilized by non-technical people. Cron, on the other hand, will fail if your system is not operating at the right execution time.

b.) Systemd Timers

Systemd Timers combine the finest features of cron. Allows for minute-level precision in scheduling. Even if the system was turned off within the intended execution period, this ensures that the job will be completed when it is restarted. All users have access to it. In the environment where it will execute, you may test and debug the execution. The configuration, on the other hand, is more complicated, needing at least two configuration files, one for the timer unit and one for the service unit.

Systemd Timers Configuration

The timetable is defined by the timer unit file, whereas the task(s) is defined by the service unit file. To execute the required functions, you’ll also need a job file or script. man systemd.timer provides further information on the timer unit. man systemd.service provides further information about the service unit. Unit files can be found in a number of places (listed in the man page). However, your service can be launched under the /etc/systemd/system/ directory.

Configuring Cron Jobs in Linux using Systemd Timers

This demonstration shows how to create a system-scheduled job. It creates a file that contains the files from the /etc directory.

1. Create a Shell Script that will Perform the Task

First of all we create a script file under /etc/systemd/system directory i.e /etc/systemd/system/my-task.sh file.

sudo touch /etc/systemd/system/my-task.sh
2. Add the Following Contents to the Script File Above

Open the script file with your favorite editor add the following contents;

sudo vim /etc/systemd/system/my-task.sh

Adding contents to the file;

#!/bin/sh
echo "This is the list of files under /etc directory: $(ls -alh /etc)" >> "/etc/myfiles.txt"

The above shell scrip will list the files of /etc directory and append to myfiles.txt file under ~/etc directory.

3. Make the Script Executable

Run the following command to make the script executable.

sudo chmod +x /etc/systemd/system/my-task.sh
4. Create the .service Unit

Make the .service unit that will run the script mentioned above. Create a file in the /etc/systemd/system/ directory.

i.e

sudo touch /etc/systemd/system/my-task.service

Edit the file above and add the following contents;

 sudo vim /etc/systemd/system/my-task.service

Contents of the above file.

[Unit]
Description=A job to list /etc directory files

[Service]
Type=simple
ExecStart=/etc/systemd/system/my-task.sh

[Install]
WantedBy=default.target

Enable the created service above;

systemctl enable my-task.service
Created symlink /etc/systemd/system/default.target.wants/my-task.service → /etc/systemd/system/my-task.service.

Now start the service by running the following command;

systemctl start my-task.service

Check the service status;

$ systemctl status my-task.service
● my-task.service - A job to list /etc directory files
     Loaded: loaded (/etc/systemd/system/my-task.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2025-02-18 14:20:01 EAT; 9s ago
TriggeredBy: ● my-task.timer
    Process: 1480009 ExecStart=/etc/systemd/system/my-task.sh (code=exited, status=0/SUCCESS)
   Main PID: 1480009 (code=exited, status=0/SUCCESS)

Jul 31 14:20:01 cloudspinx systemd[1]: Started A job to list /etc directory files.
Jul 31 14:20:01 cloudspinx systemd[1]: my-task.service: Succeeded.
5. Create the .timer Unit

Make a .timer unit file that schedules the .service unit you just made. It should be saved in the same directory as the .service file.

e.g /etc/systemd/system/ directory.

sudo touch /etc/systemd/system/my-task.timer

Edit the file above and add the following contents;

 sudo vim /etc/systemd/system/my-task.timer

Contents of the above file.

[Unit]
Description=List the files of /etc directory every minute
RefuseManualStart=no
RefuseManualStop=no

[Timer]
#Execute job if it missed a run due to machine being off
Persistent=true
#Run 120 seconds after boot for the first time
OnBootSec=120
#Run every 1 minute thereafter
OnUnitActiveSec=60
#File describing job to execute
Unit=my-task.service

[Install]
WantedBy=timers.target

Enable the created timer above;

systemctl enable my-task.timer
Created symlink /etc/systemd/system/timers.target.wants/my-task.timer → /etc/systemd/system/my-task.timer.

Now start the timer by running the following command;

systemctl start my-task.timer

Check the timer status;

$ systemctl status my-task.timer
● my-task.timer - List the files of /etc directory every minute
     Loaded: loaded (/etc/systemd/system/my-task.timer; enabled; vendor preset: enabled)
     Active: active (waiting) since Tue 2025-02-18 13:33:48 EAT; 44min ago
    Trigger: Sat 2025-02-10 14:18:45 EAT; 51s left
   Triggers: ● my-task.service

Jul 31 13:33:48 cloudspinx systemd[1]: Started List the files of /etc directory every minute.

A [Timer] section in a timer file must contain information about the timer it specifies. The following are the settings unique to the [Timer] part of timer units as per man 5 systemd.timer.

  • OnActiveSec=
  • OnBootSec=
  • OnStartupSec=
  • OnUnitActiveSec=
  • OnUnitInactiveSec=


The above defines monotonic timers relative to different starting points.

The following is Settings and their starting points.

Specifications for Calendar Event

Calendar event parameters are an important component of setting up timers to run at particular intervals. DOW YYYY-MM-DD HH:MM:SS is the fundamental format for systemd timers using OnCalendar=.

Some common time requirements used in OnCalendar statements are shown below.

Calendar event specificationDescription
WeeklyEvery Monday at 00:00:00
*-*-* 00:20:30Every day of every month of every year at 20 minutes and 30 seconds after midnight
MonSame as weekly
Wed 2025-*-*Every Wednesday in 2025 at 00:00:00
Mon *-*-* 00:00:00Same as weekly
2025-6,7,8-1,15 02:30:00The 1st and 15th of June, July, and August of 2025 at 02:30:00am
Mon..Fri *-09~05The 5th day preceding the end of September for any years in which it also falls on a weekday.
Checking Scheduled Tasks

We can now check the scheduled task above by displaying the contents of myfiles.txt file in /etc directory using cat command.

$ cat /etc/myfiles.txt | less
This is the list of files under /etc directory: total 1.4M
drwxr-xr-x 161 root  root    12K Jul 31 13:09 .
drwxr-xr-x  20 root  root   4.0K Sep  1  2020 ..
drwxr-xr-x   3 root  root   4.0K Apr  2  2020 acpi
-rw-r--r--   1 root  root   3.0K Apr  2  2020 adduser.conf
-rw-r--r--   1 root  root      0 Jun 19 09:24 aliases
drwxr-xr-x   3 root  root   4.0K Apr  2  2020 alsa
drwxr-xr-x   2 root  root    12K Jun 29 11:34 alternatives
-rw-r--r--   1 root  root    401 Jul 16  2019 anacrontab
drwxr-xr-x   8 root  root   4.0K Jul  7 08:48 apache2
-rw-r--r--   1 root  root    433 Oct  2  2017 apg.conf
drwxr-xr-x   5 root  root   4.0K Apr  2  2020 apm
drwxr-xr-x   3 root  root   4.0K Dec  5  2020 apparmor
drwxr-xr-x   8 root  root   4.0K Jul  7 08:48 apparmor.d
drwxr-xr-x   4 root  root   4.0K Jun 17 08:55 apport
-rw-r--r--   1 root  root    769 Jan 18  2020 appstream.conf
drwxr-xr-x   7 root  root   4.0K Jul  8 20:28 apt
drwxr-xr-x   3 root  root   4.0K Jul 13 08:23 avahi
-rw-r--r--   1 root  root   2.3K Feb 25  2020 bash.bashrc
-rw-r--r--   1 root  root     45 Jan 26  2020 bash_completion
drwxr-xr-x   2 root  root   4.0K Jun 17 09:22 bash_completion.d
-rw-r--r--   1 root  root    367 Jan 27  2016 bindresvport.blacklist
drwxr-xr-x   2 root  root   4.0K Mar 24  2020 binfmt.d
drwxr-xr-x   2 root  root   4.0K Jun 20 08:49 bluetooth
drwxr-xr-x   2 root  root   4.0K Jun  8 22:22 bonobo-activation
-rw-r-----   1 root  root     33 Apr  2  2020 brlapi.key
drwxr-xr-x   7 root  root   4.0K Apr  2  2020 brltty
-rw-r--r--   1 root  root    27K Mar  3  2020 brltty.conf
drwxr-xr-x   3 root  root   4.0K Apr  2  2020 ca-certificates
-rw-r--r--   1 root  root   6.5K Jun 17 09:10 ca-certificates.conf
-rw-r--r--   1 root  root   6.4K Dec  5  2020 ca-certificates.conf.dpkg-old
drwxr-xr-x   2 root  root   4.0K Apr  2  2020 calendar
drwxr-s---   2 root  dip    4.0K Sep 12  2020 chatscripts
drwxr-xr-x   2 root  root   4.0K May 22 09:54 chrony
drwxr-xr-x   2 root  root   4.0K Sep  1  2020 console-setup
drwxr-xr-x   2 root  root   4.0K Sep  4  2020 cracklib
drwxr-xr-x   2 root  root   4.0K Jun 19 09:24 cron.d
drwxr-xr-x   2 root  root   4.0K Jul  8 20:41 cron.daily
drwxr-xr-x   2 root  root   4.0K Apr  2  2020 cron.hourly
Stopping Systemd Timers

You can stop systemd timers starting with service unit followed by timer unit as follows;

#stopping the service unit
sudo systemctl stop my-task.service

#stopping the timer unit
sudo systemctl stop my-task.timer

Conclusion

This concludes our lesson on how to configure Cron Jobs in Linux using Systemd Timers. We hope you found this information useful. Stay tuned for more such lessons. Cheers!

Linux System Administration Video Courses:

Your IT Journey Starts Here!

Ready to level up your IT skills? Our new eLearning platform is coming soon to help you master the latest technologies.

Be the first to know when we launch! Join our waitlist now.

Join our Linux and open source community. Subscribe to our newsletter for tips, tricks, and collaboration opportunities!

Recent Post

Leave a Comment

Your email address will not be published. Required fields are marked *

Related Post

Google Drive is a fantastic cloud storage service that allows you to upload, share, and sync data across several computers. […]

Ceph Storage is a free and open source software-defined, distributed storage solution designed to be massively scalable for modern data […]

In day-to-day life, using a single Desktop environment can be boring and one may be required to switch to another […]

Let's Connect

Unleash the full potential of your business with CloudSpinx. Our expert solutions specialists are standing by to answer your questions and tailor a plan that perfectly aligns with your unique needs.
You will get a response from our solutions specialist within 12 hours
We understand emergencies can be stressful. For immediate assistance, chat with us now

Contact CloudSpinx today!

Download CloudSpinx Profile

Discover the full spectrum of our expertise and services by downloading our detailed Company Profile. Simply enter your first name, last name, and email address.