Blame
|
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 | ||||||||
| 19 | ``` |
|||||||
| 20 | [Unit] |
|||||||
| 21 | Description=<what the service / called script does> |
|||||||
| 22 | Wants=network-online.target |
|||||||
| 23 | After=network-online.target |
|||||||
| 24 | ||||||||
| 25 | [Service] |
|||||||
| 26 | Type=oneshot|simple |
|||||||
| 27 | RemainAfterExit=no |
|||||||
| 28 | User=<user for script> |
|||||||
| 29 | Group=<group of files?> |
|||||||
| 30 | Environment=HOME=<home directory> |
|||||||
| 31 | ExecStart=/usr/bin/flock --nonblock /tmp/<lock filename>.lock <script with absolute path> |
|||||||
| 32 | ExecStop= |
|||||||
|
33 | |||||||
| 34 | [Install] |
|||||||
| 35 | WantedBy=multi-user.target|timers.target |
|||||||
|
36 | ``` |
||||||
| 37 | - wants/after network-online.target mean that this service needs the network to be online |
|||||||
| 38 | - Type `oneshot` is for short running tasks like backup scripts, `simple` for permanently running things like server and databases |
|||||||
| 39 | - 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 |
|||||||
|
40 | - Environment could also be directory of python (inside a virtual environment) |
||||||
|
41 | - flock create lock file to prevent different backups running in parallel |
||||||
| 42 | - nonblock means, that additional starts doesn't wait but stop executing |
|||||||
|
43 | - `Restart=on-failure` means restart if service crashed |
||||||
| 44 | - `RestartSec=5` delay for automatic restart |
|||||||
| 45 | - `Install WantedBy` tells the service what to do when `systemctl enable` is called |
|||||||
|
46 | |||||||
| 47 | 2. Test service |
|||||||
| 48 | ```bash |
|||||||
| 49 | sudo systemctl daemon-reload |
|||||||
| 50 | sudo systemctl start <servicename>.service |
|||||||
| 51 | sudo systemctl status <servicename>.service |
|||||||
| 52 | ``` |
|||||||
| 53 | ||||||||
| 54 | 3. Create timer |
|||||||
| 55 | ```bash |
|||||||
| 56 | sudo nano /etc/system/system/<servicename>.timer |
|||||||
| 57 | ``` |
|||||||
| 58 | ||||||||
| 59 | ``` |
|||||||
| 60 | [Unit] |
|||||||
| 61 | Description=<name and how often as title> |
|||||||
| 62 | ||||||||
| 63 | [Timer] |
|||||||
| 64 | OnCalendar=*-*-* 03:15:00 |
|||||||
| 65 | Persistent=false |
|||||||
| 66 | Unit=<servicename>.service |
|||||||
| 67 | ||||||||
| 68 | [Install] |
|||||||
| 69 | WantedBy=timers.target |
|||||||
| 70 | ``` |
|||||||
| 71 | - OnCalendar sets time for daily start |
|||||||
| 72 | - Persistent=true would mean, that if Raspi would be offline during start time, the run will be catched up after the next start |
|||||||
| 73 | ||||||||
| 74 | 4. Enable / activate Timer |
|||||||
| 75 | ```bash |
|||||||
| 76 | sudo systemctl daemon-reload |
|||||||
| 77 | sudo systemctl enable --now <servicetimer>.timer |
|||||||
| 78 | systemctl status <servicetimer>.timer |
|||||||
| 79 | ``` |
|||||||
| 80 | - --now starts the timer after enabling |
|||||||
| 81 | ||||||||
| 82 | ## List all timers |
|||||||
| 83 | `systemctl list-timers` or `systemctl list-timers <servicename>.timer` |
|||||||
