#!/bin/sh # # Copyright (c) 2004-2007 - Consultas, PKG.fr # # This file is part of A2P. # # A2P is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # A2P is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with A2P; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # # $Id$ # # a2p-db: This is the init script for starting up a2p-status.pl # # chkconfig: 345 99 10 # description: Starts and stops a2p-status daemon # that handles job status # processname: a2p-status # pidfile: /var/run/a2p-status.pid # Source function library. . /etc/init.d/functions # See how we were called. # Strip service name from Sxx/Kxx if started during boot or during shutdown prog="`basename $0|sed -re 's/^S[0-9]{2}//' -e 's/^K[0-9]{2}//'`" pid="`pidof $prog`" pidfile="/var/run/$prog.pid" # Delete PID file if service is not running if [ -z "$pid" -a -s "$pidfile" ]; then echo "$prog service is dead" rm -f "$pidfile" fi # Optional conf integration if [ -n "$prog" -a -e "/etc/afp2print/${prog}.conf" ]; then . "/etc/afp2print/${prog}.conf" fi [ -z "$LOCKID" ] && LOCKID="`hostname -s|tr 'a-z' 'A-Z'`" export LOCKID # Perl library path options to help service to found its needed modules PERLOPT="" [ -d "$AFP2PRINT_PATH/A2P" ] && PERLOPT="$PERLOPT -I$AFP2PRINT_PATH" # Prepare Perl binary environment # Also unset JAVA_HOME in case it is in our ENV as it should exist only during # launch by Eclipse IDE, we need a LOCKID to ask process to load its conf PERLENV="unset JAVA_HOME ; LANG=C LOCKID=\"$LOCKID\"" # These defaults can be overided in confs : ${AFP2PRINT_USER:=afp2print} : ${SERVICE_PROG:=$AFP2PRINT_PATH/a2p-status.pl} : ${SERVICE_DEBUG:=0} : ${PERL:=/usr/bin/perl} : ${NICE:=nice -19} : ${LOGFILENAME:=/var/log/a2p.debug} start() { # For SELinux we need to use 'runuser' not 'su' SU=su if [ -s /etc/redhat-release ]; then if ! fgrep -q "Red Hat Enterprise Linux ES release 3" /etc/redhat-release; then [ -x /sbin/runuser ] && SU=runuser fi fi echo -n "Starting $prog: " if [ -e "/var/lock/subsys/$prog" ]; then if [ -s "$pidfile" ] && [ -e /proc/`< "$pidfile"` ]; then failure $prog echo echo "Cannot start $prog: $prog is already running." return 1 fi fi # Prepare pid file touch "$pidfile" chown $AFP2PRINT_USER "$pidfile" chmod 664 "$pidfile" # Check user will be able to output in LOGFILENAME if (( DEBUG_IN_FILE > 0 || LOGFILE_VS_SYSLOG > 0 )); then touch $LOGFILENAME chown $AFP2PRINT_USER $LOGFILENAME chmod 664 $LOGFILENAME fi # Touch the service debug file in case we need to keep some information # Usefull when debugging Perl compilation problems if (( SERVICE_DEBUG > 0 )); then touch /var/run/${prog}.debug chown $AFP2PRINT_USER /var/run/${prog}.debug if [ "$1" == "debug" ]; then PERLOPT="$PERLOPT -dt" echo -n "" >/var/run/${prog}.trace chown $AFP2PRINT_USER /var/run/${prog}.trace chmod 664 /var/run/${prog}.trace PERLENV="$PERLENV PERLDB_OPTS=\"NonStop=1 LineInfo=/var/run/${prog}.trace frame=4 dieLevel=2\"" fi else rm -f /var/run/$prog.debug fi # Start service $NICE $SU - $AFP2PRINT_USER -c "$PERLENV $PERL $PERLOPT $SERVICE_PROG /var/run/$prog.pid" # Control started process RETVAL=$? if [ $RETVAL -eq 0 ]; then touch /var/lock/subsys/$prog # 3 minutes countdown let COUNTDOWN=900 while [ ! -s "$pidfile" ] do usleep 100000 (( --COUNTDOWN < 1 )) && break done if [ -n "$1" -a -x "/usr/bin/$1" -a -s "$pidfile" ]; then pid=`"$pidfile"` if [ "$1" == "strace" ]; then strace -T -a 120 -s 64 -tt -p $pid -f -q -o /var/log/a2p.strace& elif [ "$1" == "ltrace" ]; then ltrace -C -f -T -s 64 -tt -p $pid -o /var/log/a2p.ltrace& fi fi # Success ? [ -s "$pidfile" ] && success $prog || failure $prog echo fi return $RETVAL } stop() { echo -n "Stopping $prog: " if [ ! -e "/var/lock/subsys/$prog" ]; then failure $prog echo echo "Cannot stop $prog: $prog is not running." return 1 fi let RETVAL=0 TOTALRETVAL=0 pid="`pidof $prog`" for p in $pid do # Send signal to process group kill -TERM $p RETVAL=$? let TOTALRETVAL+=RETVAL done if [ $TOTALRETVAL -eq 0 ]; then # Wait max 3 minutes for the service to update its pid to 0 when really quitting let TIMEOUT=900 while [ -s "$pidfile" ] do [ "`< $pidfile`" -eq "0" ] && break usleep 100000 (( --TIMEOUT < 1 )) && break done [ -s "$pidfile" ] && [ "`< $pidfile`" -gt 0 ] && RETVAL=1 fi if [ $RETVAL -eq 0 ]; then success $prog rm -f "/var/lock/subsys/$prog" "$pidfile" else failure $prog fi echo return $RETVAL } abort() { pid="`pidof $prog`" echo -n "Aborting any $prog process: $pid" for p in $pid do # Send signal to process group kill -ABRT -$p done rm -f /var/lock/subsys/$prog echo_success echo return 0 } a2pkill() { pid="`pidof $prog`" echo -n "Killing any $prog process: $pid" for p in $pid do # Send signal to process group kill -KILL -$p done rm -f /var/lock/subsys/$prog echo_success echo return 0 } check() { # Check first if we are called to check status file when arg is the DESTID if [ -e "$SHMDIR/.jobstatus-$1" ]; then prog="$AFP2PRINT_PATH/dbm-status-tools.pl" exec perl -I$AFP2PRINT_PATH $prog "$SHMDIR/.jobstatus-$1" fi pid="`pidof $prog`" echo -n "Debugging any $prog process: $pid" for p in $pid do # Send signal to process group kill -USR1 -$p done sleep 1 echo_success echo for file in /tmp/$prog-self$1.log do if [ -s $file ]; then name=${file/*\/} name=${name/-self$1.log} echo "====================$name====================" cat $file fi done rm -f /tmp/$prog*-self*.log return 0 } rhstatus() { status $prog } restart() { stop sleep 1 start } reload() { echo -n "Reloading $prog daemon configuration: " killproc $prog -HUP RETVAL=$? echo return $RETVAL } case "$1" in start) start $2 ;; stop) stop ;; abort) abort ;; kill) a2pkill ;; restart) restart ;; reload) reload ;; status) rhstatus ;; test) check "test" ;; debug) check "debug" ;; check) check $2 ;; condrestart) [ -f /var/lock/subsys/$prog ] && restart || : ;; *) echo "Usage: ${prog} {start|stop|abort|kill|status|reload|restart|condrestart|test|debug|check}" exit 1 esac # Check debug output OUTPUT="/var/run/${prog}.debug" if [ -s "$OUTPUT" ]; then echo "Debug output for $prog service:" # Strip output from DBI lib warning on RHEL3 egrep -v '^(.*/usr/share/mysql/charsets|Character set)' "$OUTPUT" [ "$1" == "stop" -o "$1" == "abort" -o "$1" == "kill" ] && rm -f "$OUTPUT" fi