From 616bd437e61acce21d39f19d368cded3a8d5bcc6 Mon Sep 17 00:00:00 2001 From: "Saturnin, le bot IRC" Date: Sun, 4 Nov 2012 15:56:20 +0100 Subject: [PATCH] =?utf8?q?Modification=20de=20l'initscript=20pour=20qu'il?= =?utf8?q?=20recr=C3=A9e=20le=20dossier/fichier=20de=20PID=20si=20il=20n'e?= =?utf8?q?st=20pas=20l=C3=A0=20(/var/run,=20reboot,=20tout=20=C3=A7a?= =?utf8?q?=E2=80=A6)?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- initscript | 218 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 218 insertions(+) create mode 100755 initscript diff --git a/initscript b/initscript new file mode 100755 index 0000000..c72b7b5 --- /dev/null +++ b/initscript @@ -0,0 +1,218 @@ +#! /bin/sh +### BEGIN INIT INFO +# Provides: saturnin +# Required-Start: $remote_fs $syslog +# Required-Stop: $remote_fs $syslog +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: saturnin, le bot IRC +# Description: saturnin est un bot IRC codé par 20-100 +# Il remplace le canard, qui nous manquait tant +### 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 canard" +PYTHON=/usr/bin/python +BASENAME=saturnin +DIRECTORY=/home/saturnin/saturnin +SCRIPT=$DIRECTORY/$BASENAME.py +EXEC="$PYTHON $SCRIPT" +USER=saturnin +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 -- 2.39.2