source: A2P/a2p/a2p-stat.sh @ 15

Last change on this file since 15 was 3, checked in by guillaume, 17 years ago
  • AUTHORS: Ajout des différents contributeurs
  • COPYING: Ajout de la licence GPL v3
  • a2p: Préparation des sources pour leur publication sous GPL
  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 7.1 KB
Line 
1#!/bin/sh
2#
3# Copyright (c) 2004-2007 - Consultas, PKG.fr
4#
5# This file is part of A2P.
6#
7# A2P is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# A2P is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with A2P; if not, write to the Free Software
19# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20#
21# $Id: a2p-stat.sh 3 2007-10-18 16:20:19Z guillaume $
22#
23# a2p-stat: This is the init script for starting up a2p-stat.pl
24#
25# chkconfig: 345 99 10
26# description: Starts and stops a2p-stat daemon
27#              that handles job statistics
28# processname: a2p-stat
29# pidfile: /var/run/a2p-stat.pid
30
31# Source function library.
32. /etc/init.d/functions
33
34# See how we were called.
35
36# Strip service name from Sxx/Kxx if started during boot or during shutdown
37prog="`basename $0|sed -re 's/^S[0-9]{2}//' -e 's/^K[0-9]{2}//'`"
38pid="`pidof $prog`"
39
40# Delete PID file if service is not running
41[ -z "$pid" -a -s /var/run/$prog.pid ] && rm -f /var/run/$prog.pid
42
43# Optional conf integration
44if [ -n "$prog" -a -e "/etc/afp2print/${prog}.conf" ]; then
45    . "/etc/afp2print/${prog}.conf"
46fi
47
48[ -z "$LOCKID" ] && LOCKID="`hostname -s|tr 'a-z' 'A-Z'`"
49export LOCKID
50
51# Perl library path options to help service to found its needed modules
52PERLOPT=""
53[ -d "$AFP2PRINT_PATH/A2P" ] && PERLOPT="$PERLOPT -I$AFP2PRINT_PATH"
54
55# Prepare Perl binary environment
56# Also unset JAVA_HOME in case it is in our ENV as it should exist only during
57# launch by Eclipse IDE, we need a LOCKID to ask process to load its conf
58PERLENV="unset JAVA_HOME ; LANG=C LOCKID=\"$LOCKID\""
59
60# These defaults can be overided in confs
61: ${AFP2PRINT_USER:=afp2print}
62: ${SERVICE_PROG:=$AFP2PRINT_PATH/a2p-stat.pl}
63: ${SERVICE_DEBUG:=0}
64: ${PERL:=/usr/bin/perl}
65: ${NICE:=nice -19}
66: ${LOGFILENAME:=/var/log/a2p.debug}
67
68start() {
69    # For SELinux we need to use 'runuser' not 'su'
70    SU=su
71    if [ -s /etc/redhat-release ]; then
72        if ! fgrep -q "Red Hat Enterprise Linux ES release 3" /etc/redhat-release; then
73            [ -x /sbin/runuser ] && SU=runuser
74        fi
75    fi
76
77    echo -n "Starting $prog: "
78    if [ -e /var/lock/subsys/$prog ]; then
79        if [ -s /var/run/$prog.pid ] && [ -e /proc/`< /var/run/$prog.pid` ]; then
80            echo -n "cannot start $prog: $prog is already running.";
81            failure "cannot start $prog: $prog already running.";
82            echo
83            return 1
84        fi
85    fi
86
87    # Prepare pid file
88    touch /var/run/${prog}.pid
89    chown $AFP2PRINT_USER /var/run/${prog}.pid
90    chmod 664 /var/run/${prog}.pid
91
92    # Check user will be able to output in LOGFILENAME
93    if (( DEBUG_IN_FILE > 0 || LOGFILE_VS_SYSLOG > 0 )); then
94        touch $LOGFILENAME
95        chown $AFP2PRINT_USER $LOGFILENAME
96        chmod 664 $LOGFILENAME
97    fi
98
99    # Touch the service debug file in case we need to keep some information
100    # Usefull when debugging Perl compilation problems
101    if (( SERVICE_DEBUG > 0 )); then
102        touch /var/run/${prog}.debug
103        chown $AFP2PRINT_USER /var/run/${prog}.debug
104        if [ "$1" == "debug" ]; then
105            PERLOPT="$PERLOPT -dt"
106            echo -n "" >/var/run/${prog}.trace
107            chown $AFP2PRINT_USER /var/run/${prog}.trace
108            chmod 664 /var/run/${prog}.trace
109            PERLENV="$PERLENV PERLDB_OPTS=\"NonStop=1 LineInfo=/var/run/${prog}.trace frame=4 dieLevel=2\""
110        fi
111    else
112        rm -f /var/run/$prog.debug
113    fi
114
115    # Start service
116    $NICE $SU - $AFP2PRINT_USER -c "$PERLENV $PERL $PERLOPT $SERVICE_PROG /var/run/$prog.pid"
117    RETVAL=$?
118    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog && success $prog;
119    echo
120    while [ ! -s /var/run/$prog.pid -a $RETVAL -eq 0 ]
121    do
122        usleep 100000
123    done
124    if [ -n "$1" -a -x "/usr/bin/$1" -a -s /var/run/$prog.pid ]; then
125        pid=`</var/run/$prog.pid`
126        if [ "$1" == "strace" ]; then
127            strace -T -a 120 -s 64 -tt -p $pid -f -q -o /var/log/a2p.strace&
128        elif [ "$1" == "ltrace" ]; then
129            ltrace -C -f -T -s 64 -tt -p $pid -o /var/log/a2p.ltrace&
130        fi
131    fi
132    return $RETVAL
133}
134
135stop() {
136    echo -n "Stopping $prog: "
137    if [ ! -e /var/lock/subsys/$prog ]; then
138        echo -n "cannot stop $prog: $prog is not running."
139        failure "cannot stop $prog: $prog is not running."
140        echo
141        return 1;
142    fi
143
144    let RETVAL=0 TOTALRETVAL=0
145    pid="`pidof $prog`"
146
147    for p in $pid
148    do
149        # Send signal to process group
150        kill -TERM $p
151        RETVAL=$?
152        let TOTALRETVAL+=RETVAL
153    done
154
155    [ $TOTALRETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog && echo_success;
156    # Wait max 3 minutes for the service to update its pid to 0 when really quitting
157    let TIMEOUT=10
158    while [ -s /var/run/$prog.pid -a $RETVAL -eq 0 ]
159    do
160        [ "`< /var/run/$prog.pid`" == "0" ] && break
161        usleep 100000
162        (( --TIMEOUT == 0 )) && break
163    done
164    [ -s /var/run/$prog.pid ] && [ "`< /var/run/$prog.pid`" -gt 0 -a $RETVAL -eq 0 ] && RETVAL=1
165    echo
166    return $RETVAL
167}
168
169abort() {
170    pid="`pidof $prog`"
171    echo -n "Aborting any $prog process: $pid"
172
173    for p in $pid
174    do
175        # Send signal to process group
176        kill -ABRT -$p
177    done
178
179    rm -f /var/lock/subsys/$prog
180    echo_success
181    echo
182    return 0
183}
184
185a2pkill() {
186    pid="`pidof $prog`"
187    echo -n "Killing any $prog process: $pid"
188
189    for p in $pid
190    do
191        # Send signal to process group
192        kill -KILL -$p
193    done
194
195    rm -f /var/lock/subsys/$prog
196    echo_success
197    echo
198    return 0
199}
200
201check() {
202    pid="`pidof $prog`"
203    echo -n "Debugging any $prog process: $pid"
204
205    for p in $pid
206    do
207        # Send signal to process group
208        kill -USR1 -$p
209    done
210
211    sleep 1
212
213    echo_success
214    echo
215
216    cat   /tmp/$prog*-self$1.log
217    rm -f /tmp/$prog*-self*.log
218
219    return 0
220}
221
222rhstatus() {
223    status $prog
224}
225
226restart() {
227      stop
228      sleep 1
229    start
230}
231
232reload() {
233    echo -n "Reloading $prog daemon configuration: "
234    killproc $prog -HUP
235    RETVAL=$?
236    echo
237    return $RETVAL
238}
239
240case "$1" in
241  start)
242      start $2
243    ;;
244  stop)
245      stop
246    ;;
247  abort)
248      abort
249    ;;
250  kill)
251      a2pkill
252    ;;
253  restart)
254      restart
255    ;;
256  reload)
257      reload
258    ;;
259  status)
260      rhstatus
261    ;;
262  test)
263      check "test"
264    ;;
265  debug)
266      check "debug"
267    ;;
268  check)
269      check $1
270    ;;
271  condrestart)
272      [ -f /var/lock/subsys/$prog ] && restart || :
273    ;;
274  *)
275    echo "Usage: ${prog} {start|stop|abort|kill|status|reload|restart|condrestart|test|debug}"
276    exit 1
277esac
278
279# Check debug output
280OUTPUT="/var/run/${prog}.debug"
281if [ -s "$OUTPUT" ]; then
282    echo "Debug output for $prog service:"
283    egrep -v '^(.*/usr/share/mysql/charsets|Character set)' "$OUTPUT"
284    [ "$1" == "stop" -o "$1" == "abort" -o "$1" == "kill" ] && rm -f "$OUTPUT"
285fi
Note: See TracBrowser for help on using the repository browser.