tags:

views:

1494

answers:

3

I want to launch a background Python job from a bash script and then gracefully kill it with SIGINT. This works fine from the shell, but I can't seem to get it to work in a script.

loop.py:

#! /usr/bin/env python
if __name__ == "__main__":
    try:
        print 'starting loop'
        while True:
            pass
    except KeyboardInterrupt:
        print 'quitting loop'

From the shell I can interrupt it:

$ python loop.py &
[1] 15420
starting loop
$ kill -SIGINT 15420
quitting loop
[1]+  Done                    python loop.py

kill.sh:

#! /bin/bash
python loop.py &
PID=$!
echo "sending SIGINT to process $PID"
kill -SIGINT $PID

But from a script I can't:

$ ./kill.sh 
starting loop
sending SIGINT to process 15452
$ ps ax | grep loop.py | grep -v grep
15452 pts/3    R      0:08 python loop.py

And, if it's been launched from a script I can no longer kill it from the shell:

$ kill -SIGINT 15452
$ ps ax | grep loop.py | grep -v grep
15452 pts/3    R      0:34 python loop.py

I'm assuming I'm missing some fine point of bash job control.

+2  A: 

You're not registering a signal handler. Try the below. It seems to work fairly reliably. I think the rare exception is when it catches the signal before Python registers the script's handler. Note that KeyboardInterrupt is only supposed to be raised, "when the user hits the interrupt key". I think the fact that it works for a explicit (e.g. via kill) SIGINT at all is an accident of implementation.

import signal

def quit_gracefully(*args):
    print 'quitting loop'
    exit(0);

if __name__ == "__main__":
    signal.signal(signal.SIGINT, quit_gracefully)

    try:
        print 'starting loop'
        while True:
            pass
    except KeyboardInterrupt:
        quit_gracefully()
Matthew Flaschen
+1  A: 

I agree with Matthew Flaschen; the problem is with python, which apparently doesn't register the KeyboardInterrupt exception with SIGINT when it's not called from an interactive shell.

Of course, nothing prevents you from registering your signal handler like this:

def signal_handler(signum, frame):
    raise KeyboardInterrupt, "Signal handler"
Michiel Buddingh'
A: 

When you run command in background with &, SIGINT will be ignored. Here's the relevant section of man bash:

   Non-builtin  commands  run by bash have signal handlers set to the values inherited by the shell from
   its parent.  When job control is not in effect, asynchronous commands ignore SIGINT  and  SIGQUIT  in
   addition  to  these  inherited handlers.  Commands run as a result of command substitution ignore the
   keyboard-generated job control signals SIGTTIN, SIGTTOU, and SIGTSTP.

I think you need to set signal handler explicitly as Matthew commented.

The script kill.sh also have a problem. Since loop.py is sent to background, there's no guarantee that kill runs after python loop.py.

#! /bin/bash
python loop.py &
PID=$!
#
# NEED TO WAIT ON EXISTENCE OF python loop.py PROCESS HERE.
#
echo "sending SIGINT to process $PID"
kill -SIGINT $PID
tbman