killing a script

Jason Swails jason.swails at
Mon Aug 29 22:55:55 CEST 2011

On Sun, Aug 28, 2011 at 10:41 PM, Russ P. <russ.paielli at> wrote:

> > You could look at the return value of os.system, which may tell you the
> > exit status of the process.
> Thanks for the suggestion. Yeah, I guess I could do that, but it seems
> that there should be a simpler way to just kill the "whole enchilada."
> Hitting Control-C over and over is a bit like whacking moles.

Agreed.  I had written a program that had a similar problem.  As others have
suggested, you need to either wrap os.system in another function that
analyzes the return value of the call or use another approach in which the
Python program itself sees the SIGINT (I ended up changing to
subprocess.Popen classes since they are more customizable and SIGINT is
captured by the Python program itself rather than the child process).

Another thing you can consider doing is to define your scripts' behavior if
it captures a SIGINT.


import signal, sys

def sigint_handler():
    sys.stdout.write('Caught an interruption signal!')

signal.signal(signal.SIGINT, sigint_handler)

**rest of your program**

Of course, the SIGINT signal won't be caught if it isn't seen by the main
Python process, so this still won't do anything if you use an
unprotected/unwrapped os.system command.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list