Зачем это нужно
systemd управляет службами. Unit-файл описывает ExecStart, Restart и WantedBy; после daemon-reload службу включают enable --now, а журнал смотрят journalctl -u.
Главное в работе
systemctl enable --now student-monitor запускает службу; journalctl -u student-monitor показывает записи каждую минуту.
Разбор по шагам
systemctl list-units --type=service.systemctl list-units --state=running.sudo systemctl stop nginx,sudo systemctl start nginx.sudo systemctl enable nginx.cat /lib/systemd/system/nginx.service.- Создать
/usr/local/bin/student-monitor.sh:#!/bin/bash while true; do echo "$(date) OK" >> /var/log/student-monitor.log; sleep 60; done - Создать
/etc/systemd/system/student-monitor.service:[Unit] Description=Student monitor [Service] ExecStart=/usr/local/bin/student-monitor.sh Restart=always [Install] WantedBy=multi-user.target sudo chmod +x /usr/local/bin/student-monitor.sh.sudo systemctl daemon-reload.sudo systemctl enable --now student-monitor.systemctl status student-monitor,journalctl -u student-monitor -n 20.
Шпаргалка
| Команда | Назначение |
|---|---|
systemctl list-units |
Список служб |
systemctl start/stop |
Запуск/остановка |
systemctl enable |
Автозапуск |
systemctl status |
Состояние службы |
daemon-reload |
Перечитать unit-файлы |
journalctl -u |
Журнал службы |