Configuring and Implementing Systemd Timers
Tags: systems, linux
Published: 2024-01-23 08:49:57

## Introduction

We often find ourselves needing to automate repetitive procedures when working with Linux. Normally, we use CRON Jobs for this, however Linux comes with Systemd Timer, its own scheduler for the systemd services. Systemd Timers are modern equivalent of CRON Jobs. These are timer units that allow you to plan and manage when the scripts or services run. They are frequently chosen for managing system services since they offer greater granular control.

## Explaining the systemd timers Format

Systemd Timers is a part of Systemd just like Systemd service. Systemd Timers use the `OnCalendar` for scheduling.

## `OnCalendar` Format

```
*                   *-*-*                *:*:*
Day Of the week     Year-Month-Date      Hour:Minute:Second

```

## Some Examples for `OnCalendar`

```
Explaination                          Systemd timer
Every Minute                          *-*-* *:*:00
Every 15 minutes                      *:*:0,15,30,45
Every 30 minutes                      *:*:0,30
Every 1 hour                          *-*-* *:00:00
Every other hour                      *-*-* */2:00:00
Every 12 hour                         *-*-* */12:00:00
Between certain hours                 *-*-* 9-17:00:00
Daily                                 *-*-* 00:00:00
Every Night                           *-*-* 01:00:00
Every Night at 2am                    *-*-* 02:00:00
Every morning                         *-*-* 07:00:00
Every midnight                        *-*-* 00:00:00
Every sunday                          Sun *-*-* 00:00:00
Every friday at midnight              Fri *-*-* 00:00:00
Every weekday                         Mon...Fri *-*-* 00:00:00
Every weekend                         Sat,Sun *-*-* 00:00:00
Every 7 days                          * *-*-* 00:00:00
monthly                               * *-*-01 00:00:00
Every quarter                         * *-01,04,07,10-01 00:00:00
Every 6 months                        * *-01,07-01 00:00:00
Every year                            * *-01-01 00:00:00
```

## Setup the Timer Unit

For either system-wide or user-specific timers, first create a timer units file in the directory `/etc/systemd/system` or `/etc/systemd/system`.

For this article, I’ll be use a `system-wide timer`.

## Step-1: Create system-wide timer unit file
```
$ sudo vim /etc/systemd/system/myjob.timer
```

Change “myjob” to a task-specific timer name that is descriptive.

## Step-2: Define the “.timer” file
For example, to trigger a task in every 5 minutes.
```
[Unit]
Description=My Timer

[Timer]
OnCalendar=*:*:0,15,30,45
Persistent=true
Unit=myjob.service

[Install]
WantedBy=multi-user.target
```   
> Description: Describing the use case of this timer
>     OnCalender: this line defines when the timer should trigger. In this case, it’s set to run in every 5 minutes of the day.
>     Persistent=true: This ensures that if the system is offline during a scheduled run, the task will be executed when the system is next online.
>     Unit=call the “.service” file for the timer

## Step-3: save and exit the “vim” editor.
Step-4: create system-wide service unit file
```
$ sudo vim /etc/systemd/system/myjob.service
```

## Step-5: Define the “.service” file
```
[Unit]
Description=My Job

[Service]
Type=simple
Restart=always
ExecStart=/usr/bin/python3 /<path-to-the-script>/myjob.py
```
Replace the `ExecStart` value with the command or path to the script required to be executed for the task.
Step-6: save and exit the `vim` editor.
Start & Enable the Timer

## To reload the Daemon:
```
$ sudo systemctl daemon-reload
```

## To enable the timer:
```
$ sudo systemctl enable myjob.timer
```

## To start the timer:
```
$ sudo systemctl start myjob.timer
```

## To check the timer status:
```
$ sudo systemctl status myjob.timer
```

## To restart the timer:
```
$ sudo systemctl restart myjob.timer
```

## To get the timer logs:
```
$ sudo journalctl -u myjob.service
```

## Conclusion
SystemD Timers contemporary flexibility can help us simplify our Linux system management duties. However, according on our requirements, we might have to schedule using the conventional CRON Job method.