Skip to content

Systemd-timer on Linux (multiplexar terminal)

The Systemd service manager also maintains a task scheduling service that can be used in place of cron. Some distributions no longer install cron as standard, using only the systemd-timer on Linux.

Over time, distributions have come to prefer systemd for system maintenance tasks.

Systemd uses time unit files called “Timers” that control the services or events that will be executed by systemd. These files have the extension .timer and generally reside in the /lib/systemd/system directory.

Although using systemd scheduling may be a bit more complex than using cron, there are some advantages:

  • Each task has its own time unit, or more than one if necessary;
  • Time units can call any unit, be it service, assembly, socket, etc.;
  • Time units can be executed manually;
  • Time units may depend on other services or other units to be executed, forming a logic for executing programs or services;
  • The units can be grouped;
  • The jobs performed are recorded in the systemd journal, which facilitates debugging;

Time units can be of two types:

  • Real-time timers: are triggered by a calendar event, in the same way as cron. The OnCalendar option in the time unit is used to define this type of time unit.
  • Monotonic timers: are activated after a certain amount of time (in seconds) in relation to a specific event. This event can be after the timer service is activated (OnActiveSec), after the computer is booted (OnBootSec), after the systemd service has been activated (OnStartupSec), and after a unit of time was last active (OnUnitActiveSec).

To create a systemd timer task, it is necessary to create two unit files: one for time and the other with the service or command that will be executed. First you can create the time unit:

[Unit] Description=MySQL Backup [Timer] OnCalendar=*-*-* 03:00:00 Unit=Backup.Service [Install] WantedBy=Multi-User.Target

This unit must be located in the /usr/lib/systemd/system/ directory with the name backup.timer. Notice that it will run every day at 03 in the morning, and call the unit backup.service. Now it is necessary to create the service unit that will be executed by the backup.timer unit:

[Unit] Description=MySQL Backup [Service] Type=Simple ExecStart=/var/Backup.sh [Install] WantedBy=Multi-User.Target

This file must also be located in the /usr/lib/systemd/system/ directory with the name backup.service. Note that this unit will execute the /var/backup.sh script. This script in turn must have “X” execution permission. Once this is done, you must enable the timer unit to run at boot with the “enable” option of the systemctl command:

# systemctl enable backup.timer

And also run it with the “start” option of the systemctl command:

# systemctl start backup.timer

Whenever it is necessary to change the configuration of a unit, it is necessary to restart the systemd service:

# systemctl daemon-reload

To view the time units that are configured, you can use the “list-timers” option of the systemctl command:

# systemctl list-timers NEXT LEFT LAST UNIT ACTIVATES 2019-10-19 10h 2019-10-18 backup.timer backup.service

To list all time units, even those that are not active, you can use the “list-timers —all” option:

# systemctl list-timers --all <br></br>NEXT LEFT LAST UNIT ACTIVATES <br></br>2019-10-18 8min 2019-10-18 dnf.timer dnf-makecache.service <br></br>2019-10-19 13h 2019-10-18 tmp.timer tmpfiles-clean.service <br></br>n/a n/a n/a read.timer readahead-done.service <br></br>3 timers listed.

To stop the time unit, you can use the “stop” option of the systemctl command:

# systemctl stop backup.timer

And you can run the backup.service unit at any time with the command:

# systemctl start backup.service

Passing (Transient) Time Units

Systemd can also be used to create transient (transient) time units, which will execute only once on a given date, without the need to create a service unit.

To do this, we use the systemd-run command.

Systemd-Run

The options that systemd-run accepts are:

  • —on-active=: number of seconds after the timer system became active.
  • —on-boot=: number of seconds after the computer started.
  • —on-startup=: number of seconds after systemd started.
  • —on-unit-active=: number of seconds after a unit became active
  • —on-unit-inactive=: number of seconds after a unit became inactive
  • —on-calendar=: date in the calendar for the execution of the task.

Examples:

In this example, the timer will create a file called /tmp/file after 30 seconds after it was activated:

# systemd-run --on-active=30 /bin/touch /tmp/file <br></br>Running timer as unit run-1642.timer. <br></br>It will run service as unit run-1642.service.

The timer execution can be viewed in the Systemd Journal with the journalctl command:

# journalctl -b -u run-1642.service <br></br>-- Logs begin at Tue 2019-04-09 15:46:28 -03 <br></br>Oct 18 16:05:05 systemd [1]: Started /bin/touch /tmp/archive.

To execute the updatedb command on a given date, you can use the “—on-calendar=” option after a date in quotes:

# systemd-run --on-calendar="sun 13:50 "updatedb <br></br>Running timer as unit run-2948.timer. <br></br>It will run service as unit run-2948.service.

Once the time and service unit has been created with systemd-run, you can use the “systemctl list-timers” command to see the unit timer created:

# systemctl list-timers NEXT UNIT ACTIVATES 2019-10-20 13:50 run-2948.timer run-2948.service

The contents of the unit created by systemd-run can be viewed with the command “systemctl cat run-2948”:

# systemctl cat run-2948 [Unit] description=/bin/updatedb [Service] execstart=@/bin/updatedb “/bin/updatedb”

Learn much more about Linux in our online course. You can register here. If you already have an account, or want to create one, just log in or create your user here.

Did you like it?

Share