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