Nous allons créer un service en utilisant Systemd. Il démarrera un exécutable ou un script et nous nous assurerons qu’il reste démarré en cas de problème. Créez un fichier /etc/systemd/system/myservice.service et mettez à l’intérieur.
[Unit]
Description=My service
After=network.target
StartLimitIntervalSec=0
[Service]
Type=simple
Restart=always
RestartSec=1
User=myuser
ExecStart=/path/to/my/exec/file
[Install]
WantedBy=multi-user.target
Si il s’agit d’un script, l’interpréteur doit être spécifié sur la ligne ExecStart.
ExecStart=/bin/bash /path/to/my/exec/script.sh
Maintenant nous allons le rendre exécutable, l’activer et le démarrer.
chmod 755 /etc/systemd/system/myservice.service
systemctl enable myservice
systemctl start myservice
Vérifions que notre service est démarré.
[root]$ systemctl status myservice
● myservice.service - My Service
Loaded: loaded (/etc/systemd/system/myservice.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2020-04-24 19:38:09 UTC; 3min 0s ago
Main PID: 23404 (bash)
Tasks: 49 (limit: 1143)
CGroup: /system.slice/myservice.service
├─23404 /bin/bash /path/to/my/exec/script.sh
└─23405 ./binary
Apr 24 19:38:09 server systemd[1]: Started binary service.
un chmod 755 est inutile.