]> gitweb.pimeys.fr Git - bots/basile.git/blob - initscript
Fonction full_read
[bots/basile.git] / initscript
1 #! /bin/sh
2 ### BEGIN INIT INFO
3 # Provides: basile
4 # Required-Start: $remote_fs $syslog
5 # Required-Stop: $remote_fs $syslog
6 # Default-Start: 2 3 4 5
7 # Default-Stop: 0 1 6
8 # Short-Description: Basile, le bot IRC
9 # Description: Basile est un bot IRC codé par 20-100
10 # Il est destiné à être interfacé avec la Note Kfet 2015
11 ### END INIT INFO
12
13 # Author: Vincent Le Gallic <legallic@crans.org>
14
15 # Do NOT "set -e"
16
17 # PATH should only include /usr/* if it runs after the mountnfs.sh script
18 PATH=/sbin:/usr/sbin:/bin:/usr/bin
19 DESC="bot IRC NK2015"
20 PYTHON=/usr/bin/python
21 BASENAME=basile
22 DIRECTORY=/home/basile/basile
23 SCRIPT=$DIRECTORY/$BASENAME.py
24 EXEC="$PYTHON $SCRIPT"
25 USER=basile
26 SCRIPT_ARGS="crans --daemon"
27 PIDDIR=/var/run/bots/
28 PIDFILE=$PIDDIR$BASENAME.pid
29 SCRIPTNAME=/etc/init.d/$BASENAME
30
31
32 # Load the VERBOSE setting and other rcS variables
33 . /lib/init/vars.sh
34
35 # Define LSB log_* functions.
36 # Depend on lsb-base (>= 3.2-14) to ensure that this file is present
37 # and status_of_proc is working.
38 . /lib/lsb/init-functions
39
40 _repair()
41 {
42 # Répare /var/run/bots si il était pas bon
43 mkdir -p $PIDDIR
44 chown root:bots $PIDDIR
45 chmod g+w $PIDDIR
46 }
47
48 # To check if the process is running
49 _check_is_running()
50 {
51 # On récupère le PID (écrit dans ce fichier par le script la dernière fois qu'il a été lancé)
52 pid=`cat $PIDFILE`
53 # On va voir si le process tourne toujours
54 test -x /proc/$pid || return 1 # aucun process de ce PID ne tourne
55 # On regarde si il a les bons arguments
56 egrep -q ^$PYTHON /proc/$pid/cmdline || return 1 # il y a bien un process de ce PID, mais ce n'est pas python
57 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
58 return 0
59 }
60
61 _is_running()
62 {
63 test -x $PIDDIR
64 if [ $? -ne 0 ]
65 then
66 _repair
67 fi
68 if test -f $PIDFILE
69 then
70 _check_is_running
71 return $?
72 else
73 touch $PIDFILE 2>&1 >/dev/null
74 chown $USER:bots $PIDFILE 2>&1 >/dev/null
75 if test -f $PIDFILE
76 then
77 _check_is_running
78 return $?
79 else
80 echo -n "Failed to read $PIDFILE"
81 return 0
82 fi
83 fi
84 }
85 #
86 # Function that starts the daemon
87 #
88 do_start()
89 {
90 # Return
91 # 0 if daemon has been started
92 # 1 if daemon was already running
93 # 2 if daemon could not be started
94 if _is_running
95 then
96 return 1
97 else
98 sudo -u $USER $PYTHON $SCRIPT $SCRIPT_ARGS
99 [ "$?" = 0 ] || return 2
100 fi
101 }
102
103 #
104 # Function that stops the daemon
105 #
106 do_stop()
107 {
108 # Return
109 # 0 if daemon has been stopped
110 # 1 if daemon was already stopped
111 # 2 if daemon could not be stopped
112 # other if a failure occurred
113 if _is_running
114 then
115 pid=`cat $PIDFILE`
116 kill -15 $pid
117 if [ "$?" = 0 ]
118 then
119 return 0
120 else
121 sleep 10
122 kill -9 $pid
123 fi
124 else
125 return 1
126 fi
127 }
128
129 #
130 # Function that sends a SIGHUP to the daemon
131 #
132 do_reload() {
133 # Return
134 # 0 if reload has been done
135 # 1 if daemon was not running
136 # 2 if reload could not be achieved
137 if _is_running
138 then
139 pid=`cat $PIDFILE`
140 kill -1 $pid
141 [ "$?" = 0 ] || return 2
142 else
143 return 1
144 fi
145 }
146
147 #
148 # The actual initscript
149 #
150 case "$1" in
151 start)
152 log_daemon_msg "Starting $DESC" "$BASENAME"
153 do_start
154 case "$?" in
155 0) log_end_msg 0 ;;
156 1) echo -n " was already running"; log_end_msg 1 ;;
157 2) log_end_msg 1 ;;
158 esac
159 ;;
160 stop)
161 log_daemon_msg "Stopping $DESC" "$BASENAME"
162 do_stop
163 case "$?" in
164 0) log_end_msg 0 ;;
165 1) echo -n " was not running"; log_end_msg 1 ;;
166 2) log_end_msg 1 ;;
167 esac
168 ;;
169 status)
170 if _is_running
171 then
172 log_action_msg "$BASENAME is running"
173 else
174 log_action_msg "$BASENAME is NOT running"
175 fi
176 ;;
177 reload|force-reload)
178 log_daemon_msg "Reloading $DESC" "$BASENAME"
179 do_reload
180 case "$?" in
181 0) log_end_msg 0 ;;
182 1) echo -n " was not running"; log_end_msg 1 ;;
183 2) log_end_msg 1;;
184 esac
185 ;;
186 restart)
187 log_daemon_msg "Restarting $DESC" "$BASENAME"
188 do_stop
189 case "$?" in
190 0|1)
191 do_start
192 case "$?" in
193 0) log_end_msg 0 ;;
194 1) log_end_msg 1 ;; # Old process is still running
195 *) log_end_msg 1 ;; # Failed to start
196 esac
197 ;;
198 *)
199 # Failed to stop
200 log_end_msg 1
201 ;;
202 esac
203 ;;
204 running)
205 if _is_running
206 then
207 echo "Running"
208 else
209 echo "Not running"
210 fi
211 ;;
212 *)
213 log_action_msg "Usage: $SCRIPTNAME {start|stop|status|restart|reload|force-reload}" >&2
214 exit 3
215 ;;
216 esac
217
218 true