From: Vincent Le Gallic Date: Sun, 4 Nov 2012 14:56:02 +0000 (+0100) Subject: Modification de l'initscript pour qu'il recrée le dossier/fichier de PID si il n... X-Git-Url: http://gitweb.pimeys.fr/?a=commitdiff_plain;h=d5c0fb37d2ab313de26d95101601ace77bf3e23b;p=bots%2Fdeconnaisseur.git Modification de l'initscript pour qu'il recrée le dossier/fichier de PID si il n'est pas là (/var/run, reboot, tout ça…) --- diff --git a/initscript b/initscript new file mode 100755 index 0000000..47c7a23 --- /dev/null +++ b/initscript @@ -0,0 +1,218 @@ +#! /bin/sh +### BEGIN INIT INFO +# Provides: deconnaisseur +# Required-Start: $remote_fs $syslog +# Required-Stop: $remote_fs $syslog +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: deconnaisseur, le bot IRC +# Description: deconnaisseur est un bot IRC codé par 20-100 +# Il fait jouer au jeu des déconnaissances +### END INIT INFO + +# Author: Vincent Le Gallic + +# 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="bot IRC jouant à deconnaissances" +PYTHON=/usr/bin/python +BASENAME=deconnaisseur +DIRECTORY=/home/deconnaisseur/deconnaisseur +SCRIPT=$DIRECTORY/$BASENAME.py +EXEC="$PYTHON $SCRIPT" +USER=deconnaisseur +SCRIPT_ARGS="crans --daemon" +PIDDIR=/var/run/bots/ +PIDFILE=$PIDDIR$BASENAME.pid +SCRIPTNAME=/etc/init.d/$BASENAME + + +# Load the VERBOSE setting and other rcS variables +. /lib/init/vars.sh + +# Define LSB log_* functions. +# Depend on lsb-base (>= 3.2-14) to ensure that this file is present +# and status_of_proc is working. +. /lib/lsb/init-functions + +_repair() +{ + # Répare /var/run/bots si il était pas bon + mkdir -p $PIDDIR + chown root:bots $PIDDIR + chmod g+w $PIDDIR +} + +# To check if the process is running +_check_is_running() +{ + # On récupère le PID (écrit dans ce fichier par le script la dernière fois qu'il a été lancé) + pid=`cat $PIDFILE` + # On va voir si le process tourne toujours + test -x /proc/$pid || return 1 # aucun process de ce PID ne tourne + # On regarde si il a les bons arguments + egrep -q ^$PYTHON /proc/$pid/cmdline || return 1 # il y a bien un process de ce PID, mais ce n'est pas python + grep -q $SCRIPT /proc/$pid/cmdline || return 1 # il y a bien un PID, c'est bien python, mais il ne lance pas le bot + return 0 +} + +_is_running() +{ + test -x $PIDDIR + if [ $? -ne 0 ] + then + _repair + fi + if test -f $PIDFILE + then + _check_is_running + return $? + else + touch $PIDFILE 2>&1 >/dev/null + chown $USER:bots $PIDFILE 2>&1 >/dev/null + if test -f $PIDFILE + then + _check_is_running + return $? + else + echo -n "Failed to read $PIDFILE" + return 0 + fi + fi +} +# +# Function that starts the daemon +# +do_start() +{ + # Return + # 0 if daemon has been started + # 1 if daemon was already running + # 2 if daemon could not be started + if _is_running + then + return 1 + else + sudo -u $USER $PYTHON $SCRIPT $SCRIPT_ARGS + [ "$?" = 0 ] || return 2 + fi +} + +# +# Function that stops the daemon +# +do_stop() +{ + # Return + # 0 if daemon has been stopped + # 1 if daemon was already stopped + # 2 if daemon could not be stopped + # other if a failure occurred + if _is_running + then + pid=`cat $PIDFILE` + kill -15 $pid + if [ "$?" = 0 ] + then + return 0 + else + sleep 10 + kill -9 $pid + fi + else + return 1 + fi +} + +# +# Function that sends a SIGHUP to the daemon +# +do_reload() { + # Return + # 0 if reload has been done + # 1 if daemon was not running + # 2 if reload could not be achieved + if _is_running + then + pid=`cat $PIDFILE` + kill -1 $pid + [ "$?" = 0 ] || return 2 + else + return 1 + fi +} + +# +# The actual initscript +# +case "$1" in + start) + log_daemon_msg "Starting $DESC" "$BASENAME" + do_start + case "$?" in + 0) log_end_msg 0 ;; + 1) echo -n " was already running"; log_end_msg 1 ;; + 2) log_end_msg 1 ;; + esac + ;; + stop) + log_daemon_msg "Stopping $DESC" "$BASENAME" + do_stop + case "$?" in + 0) log_end_msg 0 ;; + 1) echo -n " was not running"; log_end_msg 1 ;; + 2) log_end_msg 1 ;; + esac + ;; + status) + if _is_running + then + log_action_msg "$BASENAME is running" + else + log_action_msg "$BASENAME is NOT running" + fi + ;; + reload|force-reload) + log_daemon_msg "Reloading $DESC" "$BASENAME" + do_reload + case "$?" in + 0) log_end_msg 0 ;; + 1) echo -n " was not running"; log_end_msg 1 ;; + 2) log_end_msg 1;; + esac + ;; + restart) + log_daemon_msg "Restarting $DESC" "$BASENAME" + do_stop + case "$?" in + 0|1) + do_start + case "$?" in + 0) log_end_msg 0 ;; + 1) log_end_msg 1 ;; # Old process is still running + *) log_end_msg 1 ;; # Failed to start + esac + ;; + *) + # Failed to stop + log_end_msg 1 + ;; + esac + ;; + running) + if _is_running + then + echo "Running" + else + echo "Not running" + fi + ;; + *) + log_action_msg "Usage: $SCRIPTNAME {start|stop|status|restart|reload|force-reload}" >&2 + exit 3 + ;; +esac + +true