IT & + si affinités

Encore un blog de sysadmin … mais pas uniquement ;-)

Script init.d de gestion de service

Exemple de script de démarrage init.d, ici pour gérer le logiciel Bonitasoft, mais facilement adaptable pour n’importe quel programme qui ne serait pas fourni avec un tel script.

Le script :

#!/bin/bash

### BEGIN INIT INFO
# Provides:          update-dns
# Required-Start:    $network $syslog
# Required-Stop:     $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Manage BonitaSoft
# Description: start, stop, restart and get status of BonitaSoft
### END INIT INFO

# Provide logging functions like log_success_msg, log_failure_msg and log_warning_msg
. /lib/lsb/init-functions

[ -f /etc/default/rcS ] && . /etc/default/rcS
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/opt/pbis/bin
PROGRAM_START=/opt/BOS/bin/startup.sh
PROGRAM_STOP=/opt/BOS/bin/shutdown.sh

function countdown
{
        local OLD_IFS="${IFS}"
        IFS=":"
        local ARR=( $1 )
        local SECONDS=$((  (ARR[0] * 60 * 60) + (ARR[1] * 60) + ARR[2]  ))
        local START=$(date +%s)
        local END=$((START + SECONDS))
        local CUR=$START

        while [[ $CUR -lt $END ]]
        do
                CUR=$(date +%s)
                LEFT=$((END-CUR))

        #        printf "\r%02d:%02d:%02d" $((LEFT/3600)) $(( (LEFT/60)%60)) $((LEFT%60))
                 printf "\r%02d" $((LEFT%60))
                sleep 1
        done
        IFS="${OLD_IFS}"
        echo "        "
}

test -x $PROGRAM_START || exit 0
ID=""
case "$1" in
  start)
                ID=`ps aux |grep BONITA |grep -v grep |awk '{print $2}'`
                if [ "$ID" != "" ]; then
                        echo "Bonita is already running pid = "$ID" nothing done."
                else
                        log_begin_msg "Starting Bonitasoft..."
                        echo ""
                        $PROGRAM_START
                fi
                log_end_msg 0
                ;;
  stop)
                log_begin_msg "Stoping Bonitasoft..."
                echo ""
                $PROGRAM_STOP
                log_end_msg 0
                ;;

  restart|force-reload)
                ID=`ps aux |grep BONITA |grep -v grep |awk '{print $2}'`
                if [ "$ID" != "" ]; then
                        $0 stop
                        echo "Waiting complete stop of process"
                        echo ""
                        countdown "00:00:5"
                        $0 start
                else
                        $0 start
                fi
                ;;

  status)
                ID=`ps aux |grep BONITA |grep -v grep |awk '{print $2}'`
                if [ "$ID" != "" ]; then
                        echo "Bonita is running pid = "$ID
                else
                        echo "Bonita is not running"
                fi
                ;;
  *)
                log_failure_msg "Usage: $0 {start|stop|restart|force-reload|status}"
                exit 0
esac

exit 0

Enregistrez ce code dans un fichier ayant l’attribut exécutable ici : /etc/init.d/bonita

Activez le démarrage automatique avec la commande update-rc.d :

update-rc.d bonita defaults

Puis vous pouvez le gérer le avec la commande service :

service bonita start
service bonita status
service bonita restart
service bonita stop

Note : la fonction de compte à rebours n’est pas de moi, je l’ai trouvée ici.

Catégorie : IT