Blame

86e713 Niklas Polke 2026-08-03 20:52:15
Create SYSTEMD
1
# SYSTEMD
2
SYSTEM Daemon
3
Starts and monitors system and user services.
4
5
## Overview
6
| Command | Description |
7
| - | - |
8
| `systemctl status <serivename>.service` | Status of service (active, inactive, enabled, disabled, ...) |
9
| `systemctl list-timers <servicename>.timer` | Status of service timer (time since last run, next planned start, ...) |
10
| `/etc/systemd/system/` | Directory of self created services |
11
12
## Create timed service
13
14
1. Create service entry
15
```bash
16
sudo nano /etc/systemd/system/<servicename>.service
17
```
18
f00298 Niklas Polke 2026-08-13 13:12:53
Update Systemd Configuration
19
```bash
86e713 Niklas Polke 2026-08-03 20:52:15
Create SYSTEMD
20
[Unit]
21
Description=<what the service / called script does>
f00298 Niklas Polke 2026-08-13 13:12:53
Update Systemd Configuration
22
# Wants=network-online.target
23
# After=network-online.target
86e713 Niklas Polke 2026-08-03 20:52:15
Create SYSTEMD
24
25
[Service]
26
Type=oneshot|simple
f00298 Niklas Polke 2026-08-13 13:12:53
Update Systemd Configuration
27
# RemainAfterExit=no
86e713 Niklas Polke 2026-08-03 20:52:15
Create SYSTEMD
28
User=<user for script>
29
Group=<group of files?>
30
Environment=HOME=<home directory>
f00298 Niklas Polke 2026-08-13 13:12:53
Update Systemd Configuration
31
ExecStart=<script with absolute path>
32
# ExecStart=/usr/bin/flock --nonblock /tmp/<lock filename>.lock <script with absolute path>
33
# ExecStop=
d340b1 Niklas Polke 2026-08-03 21:01:55
Additional SYSTEMD options
34
f00298 Niklas Polke 2026-08-13 13:12:53
Update Systemd Configuration
35
# [Install]
36
# WantedBy=multi-user.target|timers.target
86e713 Niklas Polke 2026-08-03 20:52:15
Create SYSTEMD
37
```
38
- wants/after network-online.target mean that this service needs the network to be online
39
- Type `oneshot` is for short running tasks like backup scripts, `simple` for permanently running things like server and databases
40
- additional line `RemainAfterExit=yes` would be for things like `mount` which result is *active* even if the service has exited - then also an additional lne for `ExecStop` would be necessary to `unmount` the service
d340b1 Niklas Polke 2026-08-03 21:01:55
Additional SYSTEMD options
41
- Environment could also be directory of python (inside a virtual environment)
86e713 Niklas Polke 2026-08-03 20:52:15
Create SYSTEMD
42
- flock create lock file to prevent different backups running in parallel
43
- nonblock means, that additional starts doesn't wait but stop executing
d340b1 Niklas Polke 2026-08-03 21:01:55
Additional SYSTEMD options
44
- `Restart=on-failure` means restart if service crashed
45
- `RestartSec=5` delay for automatic restart
46
- `Install WantedBy` tells the service what to do when `systemctl enable` is called
86e713 Niklas Polke 2026-08-03 20:52:15
Create SYSTEMD
47
48
2. Test service
49
```bash
50
sudo systemctl daemon-reload
51
sudo systemctl start <servicename>.service
52
sudo systemctl status <servicename>.service
53
```
54
55
3. Create timer
56
```bash
57
sudo nano /etc/system/system/<servicename>.timer
58
```
59
60
```
61
[Unit]
62
Description=<name and how often as title>
63
64
[Timer]
65
OnCalendar=*-*-* 03:15:00
66
Persistent=false
67
Unit=<servicename>.service
68
69
[Install]
70
WantedBy=timers.target
71
```
72
- OnCalendar sets time for daily start
73
- Persistent=true would mean, that if Raspi would be offline during start time, the run will be catched up after the next start
74
75
4. Enable / activate Timer
76
```bash
77
sudo systemctl daemon-reload
78
sudo systemctl enable --now <servicetimer>.timer
79
systemctl status <servicetimer>.timer
80
```
81
- --now starts the timer after enabling
82
83
## List all timers
84
`systemctl list-timers` or `systemctl list-timers <servicename>.timer`