sysxplore

sysxplore

Share this post

sysxplore
sysxplore
Scheduling Tasks with Systemd Timers in Linux

Scheduling Tasks with Systemd Timers in Linux

TRÄW🤟's avatar
TRÄW🤟
Nov 02, 2024
∙ Paid
12

Share this post

sysxplore
sysxplore
Scheduling Tasks with Systemd Timers in Linux
3
Share

Scheduling tasks in Linux can be a lifesaver when you want things done automatically. For many, cron is the go-to tool for scheduling, but systemd offers a newer alternative with more flexibility and control: systemd timers. In this guide, you'll learn what systemd timers are, how they work, and how to create and manage them.

What are Systemd Timers?

In Linux, systemd timers provide a way to schedule tasks similar to cron but with more advanced features and integration into the systemd ecosystem. While cron schedules tasks purely on time intervals, systemd timers allow additional conditions like dependencies, service integration, and event-based triggers. This makes systemd timers a powerful tool for managing system tasks efficiently.

The Components of Systemd Timers

A systemd timer consists of two main parts: a timer unit and a service unit.

  • Timer Unit: This file defines when the task should run. It specifies the timing parameters, such as OnCalendar for calendar-based schedules and OnBootSec for intervals after boot time.

  • Service Unit: This file defines the actual task that will be executed. It’s where you specify what the timer is triggering, whether it’s running a script, restarting a service, or performing a cleanup operation.

These two units work together, with the timer determining the schedule and the service handling the task execution.

Why Use Systemd Timers Over Cron?

While cron is great for simple, time-based scheduling, systemd timers offer additional features that make them ideal for certain tasks:

  • Better Integration with Systemd: systemd timers can interact with other systemd units, making it easier to coordinate tasks with service dependencies.

  • Event-Based Scheduling: In addition to time-based scheduling, systemd timers support event-based scheduling with directives like OnBootSec, which lets you run tasks a specific time after boot, or OnUnitActiveSec, which schedules tasks relative to other units.

  • Persistent Scheduling: If a task is missed (for example, due to system downtime), systemd timers can be set to catch up automatically.

The following table shows the differences between systemd timers and cronjobs:

Creating a Systemd Timer

To demonstrate how systemd timers work, let’s create a simple example where a timer logs the current date and time to a file every 10 minutes. This is a straightforward way to set up a repeating task and see systemd timers in action.

Step 1: Create the Service Unit

The service unit defines the actual task. In this example, we’ll write the current date and time to a log file (/tmp/date).

Open a new service file in your preferred editor:

$ sudo nano /etc/systemd/system/date.service

Add the following content to specify the task:

[Unit]
Description=Log current date to /tmp/date

[Service]
Type=oneshot
ExecStart=/bin/sh -c 'date >> /tmp/date'

  • Description: A brief description of what the service does.

  • Type=oneshot: Indicates that the task should run once each time it’s triggered.

  • ExecStart: The command to execute. Here, we’re appending the current date to /tmp/date with each run.

Step 2: Create the Timer Unit

Now, we need a timer unit that schedules when to run date.service.

Create a new timer file:

$ sudo nano /etc/systemd/system/date.timer

Add the following content to define the timing parameters:

[Unit]
Description=Run date.service every 10 minutes

[Timer]
OnCalendar=*:0/10
Persistent=true

[Install]
WantedBy=timers.target

Keep reading with a 7-day free trial

Subscribe to sysxplore to keep reading this post and get 7 days of free access to the full post archives.

Already a paid subscriber? Sign in
© 2025 TRÄW🤟
Privacy ∙ Terms ∙ Collection notice
Start writingGet the app
Substack is the home for great culture

Share