LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Stopping Services (https://www.linuxquestions.org/questions/linux-software-2/stopping-services-515976/)

rubadub 01-03-2007 03:11 PM

Stopping Services
 
Hi,
I'm wanting to get a 'stop' (or similar) signal when the system is shutting down, so I can close my program down nicely.

I've setup a script and added it using 'chkconfig', it has both 'start' and 'stop' clauses, but it never receives a termination signal, only ever a 'start' argument. In my script this is part of what I use to govern runlevels:

# chkconfig: 2345 84 04

How, How, How?

Just for fun, here's the script. The logging is temp!:


Code:

#!/bin/bash
#
# Init file for usbtest server daemon
#
# chkconfig: 2345 84 04
# description: usbtest tester
#

dir="/home/ai/z_pylib/rck8055/usbtester_01"
script="main.py"
sname="usbtest"

logdir="/home/ai/z_pylib/rck8055/usbtester_01/log_01.txt"
echo 'CALLED:' $1 >> $logdir

case "$1" in
        start)
                cd $dir
                nohup screen -dmS $sname python $script
                echo 'START:' $1 >> $logdir
                ;;
        stop)
                process=`ps ax | grep $script | grep -v grep | grep -v SCREEN | awk '{print $1}'`
                kill -s SIGINT $process
                sleep 3
                echo 'STOP:' $1 >> $logdir
                exit
                ;;
        attach)
                screen -r $sname
                echo 'ATTACH:' $1 >> $logdir
esac
exit


P.S. The program i'm running is in python and catches the 'stop' functions with the 'SystemExit' exception, when invoked manually.

Cheers!

unSpawn 01-04-2007 10:07 AM

Quote:

stop)
process=`ps ax | grep $script | grep -v grep | grep -v SCREEN | awk '{print $1}'`
kill -s SIGINT $process
Slash the SIG part, you just need INT or KILL. BTW, the $process part could be easier if you just process=`pgrep -f main.py`, or skip even that and just ditch the two lines for one: "pkill -KILL -f main.py" (asserting there's no other processname main.py).

rubadub 01-04-2007 10:24 AM

Cheers, some good intel there!

I've not tried it yet, but...
The tempoary logging doesn't register a shutdown call what-so-ever. If I call '/sbin/service myguard off' then all works ok and with a log entry. So I believe my prob is it getting called. I was reading last night about seperate scripts for 'chkconfig' but that seems to conflict with all the example scripts i've seen. So i'm wondering if my usage of 'chkconfig' is all it should be. I added the service using '/sbin/service myguard on', and run levels set in the script.

p.s. Script actually called 'myguard' and the program 'gmon.py', sorry it's not consistent throughout these notes!

unSpawn 01-05-2007 12:39 PM

Code:

]$ file /sbin/service
/sbin/service: Bourne shell script text executable

, so that kind of equals doing "/etc/init.d/${servicename} stop". The difference between your script and the "official" ones is they
Code:

# Source function library.
. /etc/init.d/functions

which includes functions like killproc
Code:

]$ type killproc | grep success
            [ "$RC" -eq 0 ] && failure "$base shutdown" || success "$base shutdown";
                [ "$RC" -eq 0 ] && success "$base $killlevel" || failure "$base $killlevel";

which get's its "success" logline facility from:
Code:

]$ declare -f success
success ()
{
    if [ -z "${IN_INITLOG:-}" ]; then
        initlog $INITLOG_ARGS -n $0 -s "$1" -e 1;
    else
        trap "" SIGPIPE;
        echo "$INITLOG_ARGS -n $0 -s \"$1\" -e 1" 1>&21;
        trap - SIGPIPE;
    fi;
    [ "$BOOTUP" != "verbose" -a -z "$LSB" ] && echo_success;
    return 0
}

Ergo: use the functions, Luke! ;-p


All times are GMT -5. The time now is 01:10 PM.