LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 01-03-2007, 03:11 PM   #1
rubadub
Member
 
Registered: Jun 2004
Posts: 233

Rep: Reputation: 33
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!
 
Old 01-04-2007, 10:07 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,455
Blog Entries: 54

Rep: Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897
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).
 
Old 01-04-2007, 10:24 AM   #3
rubadub
Member
 
Registered: Jun 2004
Posts: 233

Original Poster
Rep: Reputation: 33
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!

Last edited by rubadub; 01-04-2007 at 11:01 AM.
 
Old 01-05-2007, 12:39 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,455
Blog Entries: 54

Rep: Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897Reputation: 2897
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
 
  


Reply

Tags
chkconfig, service, start, stop


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question regarding stopping services in RedHat 9 tuxfood Red Hat 1 05-09-2005 04:29 PM
stopping services not in xinetd.d branden_burger Linux - Security 2 04-12-2005 08:34 PM
disabling stopping services ganxteh Fedora 6 12-17-2004 10:24 PM
stopping services? piglingz Linux - Software 3 06-24-2003 04:26 PM
stopping and starting services nero64 Linux - Newbie 3 11-13-2002 01:10 AM


All times are GMT -5. The time now is 05:31 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration