Démarrer un programme au boot sous linux

Dernière modification : 2013/02/17 12:52

Soit le script python (sans faute !) lis_codebarre.py que l'on souhaite démarrer au boot en tant que service
On créé un lien symbolique dans le /usr/bin
cd /etc/bin
sudo ln -s /home/pi/programmes/lis_codebarre.py lis_codebarre

Puis on édite le script de démarrage et arrêt
sudo cp /etc/init.d/skeleton /etc/init.d/lis_codebarre
sudo nano /etc/init.d/lis_codebarre

Modification des entêtes et du chemin du programme (/usr/bin/$NAME)
### BEGIN INIT INFO
# Provides:          liscodebarre
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Example initscript
# Description:       This file should be used to construct scripts to be
#                    placed in /etc/init.d.
### END INIT INFO

# Author: Foo Bar <foobar@baz.org>
#
# Please remove the "Author" lines above and replace them
# with your own name if you copy and modify this script.

# Do NOT "set -e"

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="Lecture des codes barre"
NAME=liscodebarre
DAEMON=/usr/bin/$NAME
DAEMON_ARGS=""  #"--options args"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

Si on veux que le script de lancement gère le mode deamon et le pid pour l'arrêt, il faut ajouter --background --make-pidfile :
#
# Function that starts the daemon/service
#
do_start()
{
        # Return
        #   0 if daemon has been started
        #   1 if daemon was already running
        #   2 if daemon could not be started
        start-stop-daemon --start --quiet --background --make-pidfile --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
                || return 1
        start-stop-daemon --start --quiet --background --make-pidfile --pidfile $PIDFILE --exec $DAEMON -- \
                $DAEMON_ARGS \
                || return 2
        # Add code here, if necessary, that waits for the process to be ready
        # to handle requests from services started subsequently which depend
        # on this one.  As a last resort, sleep for some time.
}
Enfin, on update automatiquement les rc.d de chaque niveau
sudo chmod +x /etc/init.d/lis_codebarre
sudo update-rc.d lis_codebarre defaults 99

Utilisation en manuel
sudo /etc/init.d/lis_codebarre start
sudo /etc/init.d/lis_codebarre stop