====== Lancer un programme au démarrage ====== On peut souvent avoir besoin d'un programme, d'un service ou d'un script qui se lance au démarrage. Dans les dernières versions de Raspbian, c'est le système d'initialisation [[systemd]] qui est utilisé. La procédure pour démarrer un programme au démarrage n'est plus la même qu'avec les anciens systèmes d'initialisation. D'abord, je copie le script du service ssh pour partir d'une base fonctionnelle : $ sudo cp /etc/systemd/system/multi-user.target.wants/ssh.service /etc/systemd/system/multi-user.target.wants/MY-SERVICE.service Ensuite, on modifie le fichier ''MY-SERVICE.service'' : $ sudo nano /etc/systemd/system/multi-user.target.wants/MY-SERVICE.service $ sudo cat /etc/systemd/system/multi-user.target.wants/MY-SERVICE.service [Unit] Description=Script pour lancer MY-SERVICE [Service] ExecStart=/home/pi/my-service.sh [Install] WantedBy=multi-user.target Ensuite, on active le service avec la commande suivante: $ sudo systemctl enable MY-SERVICE.service Created symlink from /etc/systemd/system/multi-user.target.wants/MY-SERVICE.service to /lib/systemd/system/MY-SERVICE.service. On vérifie : $ sudo systemctl status MY-SERVICE.service (...) On relance le daemon avec systemctl, on relance le service ''MY-SERVICE'' et on vérifie son statut: $ sudo systemctl daemon-reload $ sudo systemctl restart MY-SERVICE.service $ sudo systemctl status MY-SERVICE.service (...) Un exemple pour un programme C# sous Mono qui doit tourner en permanence : [Unit] Description=Script du projet After=network.target [Service] ExecStart=/usr/bin/mono /home/pi/le-projet.exe Restart=always [Install] WantedBy=multi-user.target ===== Sources ===== * [[https://www.digitalocean.com/community/tutorials/how-to-configure-a-linux-service-to-start-automatically-after-a-crash-or-reboot-part-1-practical-examples|how to configure a linux service to start automatically after a crach or reboot (digitalocean.com)]] * [[http://www.raspberrypi-spy.co.uk/2015/10/how-to-autorun-a-python-script-on-boot-using-systemd/|how to autorun a python script on boot using systemd (raspberrypi-spy.co.uk)]] * [[https://mespotesgeek.fr/fr/execution-dun-script-interactif-au-boot-avec-systemd/|execution d'un script interactif au boot avec systemd (mespotesgeek.fr)]] * [[https://learn.adafruit.com/running-programs-automatically-on-your-tiny-computer/systemd-writing-and-enabling-a-service|running programs automatically on your tiny computer (adafruit.com)]]