100tiao1: How-to instructions you can trust. Linux How to Use Systemd Timers as a Cron Replacement

How to Use Systemd Timers as a Cron Replacement

As a Linux user, you’re probably familiar with cron. It has worked as the go-to Unix time-based job scheduler for many years. Now many users are using Systemd timers to replace cron’s dominance. This article will discuss the basics of how to set up your own systemd timer and make sure it’s running properly on your system.

Content

Why Use Systemd Timers Over Cron?

One of the biggest selling points of Systemd Timers is that it’s fully integrated into the Systemd ecosystem. This means that it’s very easy to schedule and link various Systemd service units together, making it easier to automate tasks compared to a traditional cron daemon.

Another advantage of Systemd Timers over Cron is that it provides more detailed control over how to trigger your scheduled jobs. For example, Systemd Timers have an OnBootSec variable, which allows you to delay your service unit until a certain amount of time passes after a successful boot.

Lastly, Systemd Timers are also incredibly accurate compared to a standard cron daemon. By default, it can track and run jobs up to a microsecond using its AccuracySec variable. This is incredibly helpful if you’re running a computer that requires exact timings for its scheduled tasks.

Good to know: still looking for the right task scheduling daemon? Check out our list of the best cron alternatives that you can install today.

Installing Systemd Timers

Timers are a built-in function of Systemd. This means that if you’re using Systemd as your init daemon, as is the case with popular Linux distros such as Arch, Fedora, and Ubuntu, your machine is already taking advantage of this feature on some of its system processes.

To check whether your distro is running Systemd, you can run the following command:

systemctl

Doing this will list all the Systemd units, devices, and timers that are currently running on your system.

List Existing Timers

The easiest way you can check if a timer is running on your current user account is with the following command:

systemctl list-timers

You can also use the --all flag to check every single timer in the system, including the inactive ones:

systemctl list-timers --all

Doing this will print the date and time each timer will activate, the countdown until that point, how much time has passed since it last ran, the unit name of the timer itself, and the service each timer unit activates.

Good to know: learn how to easily manage and control multiple Systemd units using chkservice.

Creating a New Service and Timer

The first step in making a Systemd timer is to either create or find an existing service to attach it to. This is because every timer needs to be attached to a parent service unit. This section will guide you through the process of creating a timer for a custom Systemd service.

Start by creating the blank “.service” and “.timer” files on your home directory:

touch ~/my-new-timer.{service,timer}

Open your new service file using your favorite text editor:

nano ~/my-new-timer.service

Paste the following template inside your new service file:

[Unit]
Name=My New Service
Description=The unit file for my custom command.
 
[Service]
Type=oneshot
User=ramces 
WorkingDirectory=/home/ramces/
ExecStart=/home/ramces/.local/bin/my-script.sh
 
[Install]
WantedBy=multi-user.target

Save your “.service” file, then open your blank “.timer” file using the same text editor:

nano ~/my-new-timer.timer

Paste the following block of code inside your timer file:

[Unit]
Name=My New Service
Description=The timer for my custom command.
 
[Timer]
Unit=my-new-timer.service
OnCalendar=*-*-* 11:43:00
Persistent=true
 
[Install]
WantedBy=timers.target

Go to the [Timer] section, then replace the value of the OnCalendar variable with the date and time that you want your Systemd unit to run. In my case, I want my program to run at exactly 11:43 AM every day.

Note: The three asterisks before the “11:43:00” value stands for Year, Month, and Day. For instance, setting this value to “09-15-24” will tell Systemd to only run your service once on September 15, 2024.

Creating a Monotonic Timer

Aside from setting a timer that runs on a specific date, it’s also possible to create a monotonic timer that runs relative to your computer’s current uptime. This is useful if you’re scheduling a task on a machine that’s not constantly running, such as on a desktop or a laptop.

To start, open your “.timer” file, then scroll down to the [Timer] section.

Remove both the OnCalendar and Persistent variables, then add OnBootSec followed by a time interval for when you want to run your unit file. For example, setting it to “1hr” will tell Systemd to start your command an hour after your computer successfully boots up.

Add the OnUnitActiveSec variable just below OnBootSec, then provide the interval that you want your timer to continuously run after your system boots. In my case, I will set it to “30min” since I want my task to run every 30 minutes after its initial run.

Creating a Transient Timer

One of the benefits of Systemd Timer being deeply integrated into Systemd is that it can take advantage of the init daemon’s other adjacent programs. For instance, it’s possible to use timers with Systemd-run to create one-time transient jobs on your machine.

To create a simple temporary timer, paste the following line of code in a terminal session, then replace the value of the --on-calendar variable with the interval that you want your command to run:

sudo systemd-run --on-calendar="*-*-* 11:46:00"

Append the command that you want to run at the end of the line, then press Enter to schedule it.

sudo systemd-run --on-calendar="*-*-* 11:46:00" /home/ramces/.local/my-script.sh

You can also create temporary monotonic timers using both the --on-boot and --on-unit-active variables:

sudo systemd-run --on-boot="1hr" --on-unit-active="30m" /home/ramces/.local/my-script.sh

Confirm that your temporary timer is now queued on Systemd’s job scheduler by running the following command:

sudo systemctl status run*.timer

On a side note: Systemd-run is more than just a way to create temporary timers in Linux. Learn how you can use it as a sudo alternative with Run0.

Running the Systemd Timer

To run your new Systemd Timer, you need to make sure that it’s properly loaded on your current Systemd session. For that, copy both your custom service and timer file to your Systemd’s service directory:

sudo cp /home/ramces/my-new-timer.{service,timer} /etc/systemd/system/

Reload the init daemon’s current configuration:

sudo systemctl daemon-reload

Activate both the service and timer you’ve created using systemctl:

sudo systemctl enable my-new-timer.service<br>sudo systemctl enable --now my-new-timer.timer

Check if you’ve loaded your new timer properly by running: sudo systemctl status my-new-timer.timer on a new terminal session. In this case, you can see that my timer is now scheduled and prepared for the system to run.

Learning how to schedule tasks using Systemd Timers is just one part of understanding how your Linux machine works under the hood. Dive deeper into the internals of this wonderful OS by looking at our comparison article on Grub and Systemd-boot.

Image credit: Alexandr Podvalny via Unsplash. All alterations and screenshots by Ramces Red.


Ramces Red
Staff Writer

Ramces is a technology writer that lived with computers all his life. A prolific reader and a student of Anthropology, he is an eccentric character that writes articles about Linux and anything *nix.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Sign up for all newsletters.
By signing up, you agree to our Privacy Policy and European users agree to the data transfer policy. We will not share your data and you can unsubscribe at any time. Subscribe

Related Post