Guia de uso systemd

Comandos

systemctl enable vgaoff.service
systemctl disable foo.service
systemctl start foo.service
systemctl stop foo.service
systemctl status foo.service

Ejecución de tareas al inicio

https://unix.stackexchange.com/questions/47695/how-to-write-startup-script-for-systemd

Ejemplo de ejecución como servicio del lector serial domótico

Crear un archivo .service en /etc/systemd/system:

[Unit]
Description=Lector del gateway domotico
StartLimitIntervalSec=60
StartLimitBurst=2
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/leeSERIAL.py /dev/ttyUSB0
Restart=on-failure


[Install]
WantedBy=multi-user.target

Para que se ejecute al inicio:
systemctl enable leeSERIAL.service

Para controlar el nuevo servicio:
systemctl [status|start|stop] leeSERIAL.service

Ejecución a la crontab (systemd timers)

Los timers equivalentes a crontab son los Real Time Timer

Ver los timers configurados
systemctl list-timers

Historial de un timer
journalctl -u [nombre del timer].timer

Instalar e iniciar un timer --> importante la extensión '.timer'


systemctl enable [nombre del timer].timer systemctl start [nombre del timer].timer

detener y desinstalar un timer
systemctl stop [nombre del timer].timer
systemctl disable[nombre del timer].timer

Creación del fichero de servicio

Un servicio puede ser llamado por más de un timer

En /etc/systemd/system/[nombre del servicio].service (debe ser world-readable)

[Unit]
Description=Descripción del servicio (tarea)

[Service]
User=root

# Qué ejecutamos
ExecStart=/home/user/bin/some_shell_script.sh

Creación del fichero de timer

Formato del OnCalendar
https://www.freedesktop.org/software/systemd/man/systemd.time.html

Configuración de un timer en /etc/systemd/system/[nombre del timer].timer (debe ser world-readable)

[Unit]
Description=Proceso diario

[Timer]

# Formato AAAA-MM-DD HH:MM
# OnCalendar=*-*-* 6,18:00

OnCalendar=daily

# aleatorizar el inicio
RandomizedDelaySec=5min

# que servicio ejecutar (con el .service)
Unit=[nombre del servicio]

# Recupera una ejecución perdida
Persistent=true

[Install]
WantedBy=timers.target

Configuración de un servicio

https://jonarcher.info/2015/08/ensure-systemd-services-restart-on-failure/

En esta página buscar por Restart:
https://www.freedesktop.org/software/systemd/man/systemd.service.html

Opciones interesantes:

Restart=