killing a script

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


On Sun, Aug 28, 2011 at 10:41 PM, Russ P. <russ.paielli at gmail.com> 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.

(Untested)

import signal, sys

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

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.

HTH,
Jason
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110829/2355e721/attachment.html>


More information about the Python-list mailing list