source: A2P/a2p/a2p-status.sh @ 8

Last change on this file since 8 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.8 KB
RevLine 
[3]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-status.sh 3 2007-10-18 16:20:19Z root $
22#
23# a2p-db: This is the init script for starting up a2p-status.pl
24#
25# chkconfig: 345 99 10
26# description: Starts and stops a2p-status daemon
27#              that handles job status
28# processname: a2p-status
29# pidfile: /var/run/a2p-status.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`"
39pidfile="/var/run/$prog.pid"
40
41# Delete PID file if service is not running
42if [ -z "$pid" -a -s "$pidfile" ]; then
43    echo "$prog service is dead"
44    rm -f "$pidfile"
45fi
46
47# Optional conf integration
48if [ -n "$prog" -a -e "/etc/afp2print/${prog}.conf" ]; then
49    . "/etc/afp2print/${prog}.conf"
50fi
51
52[ -z "$LOCKID" ] && LOCKID="`hostname -s|tr 'a-z' 'A-Z'`"
53export LOCKID
54
55# Perl library path options to help service to found its needed modules
56PERLOPT=""
57[ -d "$AFP2PRINT_PATH/A2P" ] && PERLOPT="$PERLOPT -I$AFP2PRINT_PATH"
58
59# Prepare Perl binary environment
60# Also unset JAVA_HOME in case it is in our ENV as it should exist only during
61# launch by Eclipse IDE, we need a LOCKID to ask process to load its conf
62PERLENV="unset JAVA_HOME ; LANG=C LOCKID=\"$LOCKID\""
63
64# These defaults can be overided in confs
65: ${AFP2PRINT_USER:=afp2print}
66: ${SERVICE_PROG:=$AFP2PRINT_PATH/a2p-status.pl}
67: ${SERVICE_DEBUG:=0}
68: ${PERL:=/usr/bin/perl}
69: ${NICE:=nice -19}
70: ${LOGFILENAME:=/var/log/a2p.debug}
71
72start() {
73    # For SELinux we need to use 'runuser' not 'su'
74    SU=su
75    if [ -s /etc/redhat-release ]; then
76        if ! fgrep -q "Red Hat Enterprise Linux ES release 3" /etc/redhat-release; then
77            [ -x /sbin/runuser ] && SU=runuser
78        fi
79    fi
80
81    echo -n "Starting $prog: "
82    if [ -e "/var/lock/subsys/$prog" ]; then
83        if [ -s "$pidfile" ] && [ -e /proc/`< "$pidfile"` ]; then
84            failure $prog
85            echo
86            echo "Cannot start $prog: $prog is already running."
87            return 1
88        fi
89    fi
90
91    # Prepare pid file
92    touch "$pidfile"
93    chown $AFP2PRINT_USER "$pidfile"
94    chmod 664 "$pidfile"
95
96    # Check user will be able to output in LOGFILENAME
97    if (( DEBUG_IN_FILE > 0 || LOGFILE_VS_SYSLOG > 0 )); then
98        touch $LOGFILENAME
99        chown $AFP2PRINT_USER $LOGFILENAME
100        chmod 664 $LOGFILENAME
101    fi
102
103    # Touch the service debug file in case we need to keep some information
104    # Usefull when debugging Perl compilation problems
105    if (( SERVICE_DEBUG > 0 )); then
106        touch /var/run/${prog}.debug
107        chown $AFP2PRINT_USER /var/run/${prog}.debug
108        if [ "$1" == "debug" ]; then
109            PERLOPT="$PERLOPT -dt"
110            echo -n "" >/var/run/${prog}.trace
111            chown $AFP2PRINT_USER /var/run/${prog}.trace
112            chmod 664 /var/run/${prog}.trace
113            PERLENV="$PERLENV PERLDB_OPTS=\"NonStop=1 LineInfo=/var/run/${prog}.trace frame=4 dieLevel=2\""
114        fi
115    else
116        rm -f /var/run/$prog.debug
117    fi
118
119    # Start service
120    $NICE $SU - $AFP2PRINT_USER -c "$PERLENV $PERL $PERLOPT $SERVICE_PROG /var/run/$prog.pid"
121
122    # Control started process
123    RETVAL=$?
124    if [ $RETVAL -eq 0 ]; then
125        touch /var/lock/subsys/$prog
126
127        # 3 minutes countdown
128        let COUNTDOWN=900
129        while [ ! -s "$pidfile" ]
130        do
131            usleep 100000
132            (( --COUNTDOWN < 1 )) && break
133        done
134
135        if [ -n "$1" -a -x "/usr/bin/$1" -a -s "$pidfile" ]; then
136            pid=`"$pidfile"`
137            if [ "$1" == "strace" ]; then
138                strace -T -a 120 -s 64 -tt -p $pid -f -q -o /var/log/a2p.strace&
139            elif [ "$1" == "ltrace" ]; then
140                ltrace -C -f -T -s 64 -tt -p $pid -o /var/log/a2p.ltrace&
141            fi
142        fi
143
144        # Success ?
145        [ -s "$pidfile" ] && success $prog || failure $prog
146        echo
147    fi
148    return $RETVAL
149}
150
151stop() {
152    echo -n "Stopping $prog: "
153    if [ ! -e "/var/lock/subsys/$prog" ]; then
154        failure $prog
155        echo
156        echo "Cannot stop $prog: $prog is not running."
157        return 1
158    fi
159
160    let RETVAL=0 TOTALRETVAL=0
161    pid="`pidof $prog`"
162
163    for p in $pid
164    do
165        # Send signal to process group
166        kill -TERM $p
167        RETVAL=$?
168        let TOTALRETVAL+=RETVAL
169    done
170
171    if [ $TOTALRETVAL -eq 0 ]; then
172        # Wait max 3 minutes for the service to update its pid to 0 when really quitting
173        let TIMEOUT=900
174        while [ -s "$pidfile" ]
175        do
176            [ "`< $pidfile`" -eq "0" ] && break
177            usleep 100000
178            (( --TIMEOUT < 1 )) && break
179        done
180        [ -s "$pidfile" ] && [ "`< $pidfile`" -gt 0 ] && RETVAL=1
181    fi
182
183    if [ $RETVAL -eq 0 ]; then
184        success $prog
185        rm -f "/var/lock/subsys/$prog" "$pidfile"
186    else
187        failure $prog
188    fi
189    echo
190    return $RETVAL
191}
192
193abort() {
194    pid="`pidof $prog`"
195    echo -n "Aborting any $prog process: $pid"
196
197    for p in $pid
198    do
199        # Send signal to process group
200        kill -ABRT -$p
201    done
202
203    rm -f /var/lock/subsys/$prog
204    echo_success
205    echo
206    return 0
207}
208
209a2pkill() {
210    pid="`pidof $prog`"
211    echo -n "Killing any $prog process: $pid"
212
213    for p in $pid
214    do
215        # Send signal to process group
216        kill -KILL -$p
217    done
218
219    rm -f /var/lock/subsys/$prog
220    echo_success
221    echo
222    return 0
223}
224
225check() {
226    # Check first if we are called to check status file when arg is the DESTID
227    if [ -e "$SHMDIR/.jobstatus-$1" ]; then
228        prog="$AFP2PRINT_PATH/dbm-status-tools.pl"
229        exec perl -I$AFP2PRINT_PATH $prog "$SHMDIR/.jobstatus-$1"
230    fi
231
232    pid="`pidof $prog`"
233    echo -n "Debugging any $prog process: $pid"
234
235    for p in $pid
236    do
237        # Send signal to process group
238        kill -USR1 -$p
239    done
240
241    sleep 1
242
243    echo_success
244    echo
245
246    for file in /tmp/$prog-self$1.log
247    do
248        if [ -s $file ]; then
249            name=${file/*\/}
250            name=${name/-self$1.log}
251            echo "====================$name===================="
252            cat $file
253        fi
254    done
255
256    rm -f /tmp/$prog*-self*.log
257    return 0
258}
259
260rhstatus() {
261    status $prog
262}
263
264restart() {
265      stop
266      sleep 1
267    start
268}
269
270reload() {
271    echo -n "Reloading $prog daemon configuration: "
272    killproc $prog -HUP
273    RETVAL=$?
274    echo
275    return $RETVAL
276}
277
278case "$1" in
279  start)
280      start $2
281    ;;
282  stop)
283      stop
284    ;;
285  abort)
286      abort
287    ;;
288  kill)
289      a2pkill
290    ;;
291  restart)
292      restart
293    ;;
294  reload)
295      reload
296    ;;
297  status)
298      rhstatus
299    ;;
300  test)
301      check "test"
302    ;;
303  debug)
304      check "debug"
305    ;;
306  check)
307      check $2
308    ;;
309  condrestart)
310      [ -f /var/lock/subsys/$prog ] && restart || :
311    ;;
312  *)
313    echo "Usage: ${prog} {start|stop|abort|kill|status|reload|restart|condrestart|test|debug|check}"
314    exit 1
315esac
316
317# Check debug output
318OUTPUT="/var/run/${prog}.debug"
319if [ -s "$OUTPUT" ]; then
320    echo "Debug output for $prog service:"
321    # Strip output from DBI lib warning on RHEL3
322    egrep -v '^(.*/usr/share/mysql/charsets|Character set)' "$OUTPUT"
323    [ "$1" == "stop" -o "$1" == "abort" -o "$1" == "kill" ] && rm -f "$OUTPUT"
324fi
Note: See TracBrowser for help on using the repository browser.