eolas/zk/Timed_systemd_units.md

63 lines
1.1 KiB
Markdown
Raw Permalink Normal View History

2025-11-06 18:13:33 +00:00
---
tags:
- Linux
- procedural
- systemd
---
# Timed `systemd` units
In order to use [systemd](./systemd.md) in the manner of [cron](./Cron.md) (but
with added benefits, such as being able to set the user to run as, environment
variables and so on), you first create a timer template, e.g.:
```
# everyhour@.timer
[Unit]
Description=Run %i every hour
[Timer]
OnCalendar=*-*-* *:00:00
Persistent=true
Unit=%i.service
[Install]
WantedBy=timers.target
```
You can then pass in specific units using this template to `systemctl`.
As an example, this unit runs a simple bash script that pings a server:
```
# ping-eolas-api.service
[Unit]
Description=Ping Eolas API to check it is up
[Service]
User=thomas
Group=thomas
Type=oneshot
ExecStart=/home/thomas/self-host/scripts/ping_eolas-api.sh
```
Then when enabling the service, do the usual:
```sh
systemctl daemon-reload
```
But when enabling/starting, invoke the timer template, viz:
```sh
systemctl enable everyhour@ping-eolas-api.timer
systemctl start everyhour@ping-eolas-api.timer
```
The schema is:
```
[timer_template_name][service_name].template
```