Terminating python script easily

B

Balban

Hi,

I have a python build script that calls various commands, some using
os.system().

Often, if I want to terminate the script prematurely, I press ctrl-c,
but I have to do this many times before I can kill the script for
good. I was wondering is there a way that I define a signal handler
and kill the whole thing at once with a single ctrl-c? Perhaps I
should also call my other scripts with a method other than os.system
() as well?

Thank you,

Bahadir
 
B

Benjamin Kaplan

Hi,

I have a python build script that calls various commands, some using
os.system().

Often, if I want to terminate the script prematurely, I press ctrl-c,
but I have to do this many times before I can kill the script for
good. I was wondering is there a way that I define a signal handler
and kill the whole thing at once with a single ctrl-c? Perhaps I
should  also call my other scripts with a method other than os.system
() as well?

Thank you,

Bahadir
--
In Python, ctrl-C raises a KeyboarInterrupt. You can just catch that
and terminate everything in the except clause. If you're using 2.6,
subprocess.Popen has a terminate method you can use to quit the other
scripts if you use that instead of os.system.
 
J

Jean-Michel Pichavant

Balban said:
Hi,

I have a python build script that calls various commands, some using
os.system().

Often, if I want to terminate the script prematurely, I press ctrl-c,
but I have to do this many times before I can kill the script for
good. I was wondering is there a way that I define a signal handler
and kill the whole thing at once with a single ctrl-c? Perhaps I
should also call my other scripts with a method other than os.system
() as well?

Thank you,

Bahadir
you may want to use subprocess instead of os.system.
On catching CTRL+C, you kill all the pid started with subprocess and
exit the script smoothly.

JM
 
B

Bahadir Balban

you may want to use subprocess instead of os.system.
On catching CTRL+C, you kill all the pid started with subprocess and exit
the script smoothly.

JM

Hmm. OK, this is what I suspected I needed. So no explicit signal
catching is required I guess.

I will look into it, thanks.


Bahadir
 
G

Gabriel Genellina

En Thu, 22 Oct 2009 10:03:51 -0300, Bahadir Balban
Hmm. OK, this is what I suspected I needed. So no explicit signal
catching is required I guess.

Note that KeyboardInterrupt (the exception generated by pressing ^C) may
be catched (sometimes inadvertedly) if the code uses a bare 'except'
clause, and then the program continues normally, effectively ignoring ^C.
The most generic 'except' clause should be, normally:
try: ...
except Exception: ...
 
G

Gabriel Genellina

En Thu, 22 Oct 2009 10:03:51 -0300, Bahadir Balban
Hmm. OK, this is what I suspected I needed. So no explicit signal
catching is required I guess.

Note that KeyboardInterrupt (the exception generated by pressing ^C) may
be catched (sometimes inadvertedly) if the code uses a bare 'except'
clause, and then the program continues normally, effectively ignoring ^C.
The most generic 'except' clause should be, normally:
try: ...
except Exception: ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top