User Tools

Site Tools


server:systemd_timers

Systemd Timers

This guide will detail how to properly configure and enable systemd timers.

Many thanks to this source

As I went about trying to set them up, I had the hardest time, since it seems the required information is spread around in various places. I wanted to record what I did so firstly, I can remember, but also so that others don’t have to go searching as far and wide as I did.

There are additional options associated with the each step I mention below, but this is the bare minimum to get started. Look at the man pages for systemd.service, systemd.timer, and systemd.target for all that you can do with them.

Running a Single Script

Let’s say you have a script '/usr/local/bin/myscript' that you want to run every hour.

Service File

First, create a service file, and put it wherever it goes on your Linux distribution (on Arch, it is either /etc/systemd/system/ or /usr/lib/systemd/system).

[Unit]
Description=MyScript

[Service]
ExecStart=/usr/local/bin/myscript

Timer File

Next, create a timer file, and put it also in the same directory as the service file above.

[Unit]
Description=Runs myscript every hour

[Timer]
# Time to wait after booting before we run first time
OnBootSec=10min
# Time between running each consecutive time
OnUnitActiveSec=1h
Unit=myscript.service

[Install]
WantedBy=multi-user.target

Enable / Start

Rather than starting / enabling the service file, you use the timer.

$ sudo systemctl start myscript.timer
$ sudo systemctl enable myscript.timer
server/systemd_timers.txt · Last modified: 2021/06/18 16:36 by 127.0.0.1