Timers in systemd

It’s time to see an example on timers in systemd.

Before we start, let’s clarify some things.

systemd’s Timers are units (the simplest form of a systemd files) that can describe when and if a unit service should or must run, based on real time or relative time.

A real time example is similar to a cron entry and it’s mostly declared with OnCalendar statement on timer.
You can read what values the OnCalendar= configuration setting can have, here.

A relative time example is something like: run this unit service ten minutes after boot but this Before this unit service and it depends on that unit service.


To find out what timers have been declared on your system, you can run the below command:

$ systemctl list-timers


ok, let’s start an example.

I will use, /usr/local/bin as the directory for my custom scripts, as this directory is on PATH enviroment variable and i can run the scripts from everywhere.
Our systemd unit files, must exist under: /etc/systemd/system/ as this directory is for the user to create their own units.


Part One: The Script

I want to sync my emails from one system to another. The basic script is something like this:

# /usr/bin/rsync -rax /backup/Maildir/ -e ssh homepc:/mnt/backup/Maildir/ &> /dev/null

Create a shell script with your command:

# vim /usr/local/bin/backup.maildir

/usr/bin/rsync -rax /backup/Maildir/ -e ssh homepc:/mnt/backup/Maildir/ &> /dev/null

and make it executable:

# chmod +x /usr/local/bin/backup.maildir

You can run this script, once or twice to see if everything goes as planned.

Part Two: The Service

Now it’s time to create a systemd service unit:

# vim /etc/systemd/system/backupmaildir.service

Description=Backup Maildir Service


Part Three: The Timer

Now it is time to create the systemd timer unit:

# vim /etc/systemd/system/backup.maildir.timer

We have to decide when we want to service to run.
eg. Every hour, but 15minutes after boot

Description=backup.maildir, Runs every hour



voila !

Part Four: Enabling it

Be aware, we havent finished yet.

Check that systemd can identify these files:

# systemctl list-unit-files | grep maildir
rsync.maildir.service                      static
rsync.maildir.timer                        disabled

We can run the systemd service by hand:

# systemctl start rsync.maildir.service

If you change the rsync options to ravx -P you can see the output via

# journalctl -f &
# systemctl start rsync.maildir.service

With & I put the journalctl to the background. Type fg to bring the process to foreground.

finally we need to start & enable (so that runs after reboot) the timer:

# systemctl start rsync.maildir.timer

# systemctl enable rsync.maildir.timer
Created symlink from /etc/systemd/system/multi-user.target.wants/rsync.maildir.timer to /etc/systemd/system/rsync.maildir.timer.

after that:

# systemctl list-timers  | grep maildir
Fri 2015-07-17 18:42:30 UTC  59min left Fri 2015-07-17 17:42:30 UTC  40s ago rsync.maildir.timer          rsync.maildir.service

# systemctl list-unit-files | grep maildir
rsync.maildir.service                      static
rsync.maildir.timer                        enabled

To all the systemd haters, I KNOW, its one line on crontab !

Tag(s): systemd, timer