Managing Scheduled Tasks with Systemd Timers in Linux

Introduction to Systemd Timers

Systemd timers, integral to the systemd init system in Linux, serve as a replacement to cron for task scheduling. Offering enhanced functionality and flexibility, systemd timers allow users to schedule tasks to run periodically or at specific intervals, catering to diverse system automation needs.

Understanding Systemd Service and Timer Syntax

Systemd timer units consist of two primary components:

  1. Timer Unit: Dictates the schedule and frequency of task execution.
  2. Service Unit: Specifies the task to be executed.

Timer Unit Syntax

The timer unit syntax allows you to define the timing and persistence of task execution. Let's delve into some examples.

In this example:

  • Unit specifies the systemd service that will be run when the timer is activated, in this case example.service.
  • OnCalendar dictates when the task should execute, set here to run every hour in the following syntax DayOfWeek Year-Month-Day Hour:Minute:Second, 1h would also be a valid value.
1[Unit]
2Description=Run task every hour
3
4[Timer]
5OnCalendar=* *-*-* *:00:00
6Unit=example.service
7
8[Install]
9WantedBy=timers.target

In this example:

  • OnCalendar is set to weekly which will run every Monday at 12am.
  • Persistent=true ensures task execution even if it's missed due to the system being off.
 1[Unit]
 2Description=Run task every week
 3
 4[Timer]
 5OnCalendar=weekly
 6Persistent=true
 7Unit=example.service
 8
 9[Install]
10WantedBy=timers.target

Service Unit Syntax

The service unit delineates the specific task to be executed. Here's a service unit example:

1[Unit]
2Description=Task to be executed
3
4[Service]
5Type=oneshot
6ExecStart=/path/to/script.sh

Creating and Managing Systemd Timers

1. Crafting Timer and Service Units

Initiate the creation of timer and service units using a text editor such as nano:

1user@machine:~$ sudo nano /etc/systemd/system/my-task.timer
2user@machine:~$ sudo nano /etc/systemd/system/my-task.service

2. Load and Enable Timer and Service Units

1user@machine:~$ sudo systemctl daemon-reload
2user@machine:~$ sudo systemctl start my-task.timer
3user@machine:~$ sudo systemctl enable my-task.timer

Troubleshooting Systemd Timers

1. Verify Syntax

1user@machine:~$ systemd-analyze verify /etc/systemd/system/my-task.*

2. View Timer Status

1user@machine:~$ sudo systemctl status my-task.timer

3. Reviewing Timer Logs

Examine logs pertinent to systemd timers via the journalctl command:

1user@machine:~$ journalctl -u my-task.timer

4. Validating Executable Path

Ensure the accuracy of the executable path specified in the service unit and confirm that the file's executable permissions are correct.

Conclusion

Systemd timers serve as an indispensable tool for scheduling and automating tasks within Linux environments. By grasping the systemd timer and service syntax, and effectively troubleshooting common issues, users can harness the full potential of systemd timers to optimise task scheduling and streamline system administration processes on their Linux systems.