Terminating python script easily
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Terminating python script easily
On Thu, Oct 22, 2009 at 8:46 AM, Balban bilgehan.bal...@gmail.com wrote: 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: Terminating python script easily
Balban wrote: 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Terminating python script easily
On Thu, Oct 22, 2009 at 4:01 PM, Jean-Michel Pichavant jeanmic...@sequans.com wrote: Balban wrote: 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 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Terminating python script easily
On 22Oct2009 16:03, Bahadir Balban bilgehan.bal...@gmail.com wrote: | On Thu, Oct 22, 2009 at 4:01 PM, Jean-Michel Pichavant | jeanmic...@sequans.com wrote: | Balban wrote: | 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? | | 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. | | Hmm. OK, this is what I suspected I needed. So no explicit signal | catching is required I guess. But the funny thing is that this shouldn't be necessary. On a UNIX system, ^C sends SIGINT to all the processes in the process group on the terminal. Only those who have specially caught SIGINT will fail to exit. Of course, plenty of programs do catch SIGINT for tidy-up purposes, but all who do should be tidying up and then exiting promptly. Unless the subprocess module specially protects its children from SIGINT I would _hope_ they'd all just quit. Obviously that's not the case though. I don't speak with knowledge of what subprocess does, only knowledge of what should happen on a UNIX system via ^C and what _does_ happen on a UNIX system unless some app goes out of its way to break^Wchange that behaviour. I do know that ^C doesn't always terminate my threaded python stuff cleanly, and I think I recall that such stuff is only delivered to the main thread of a python program, thus requiring special handle to tidy up other stuff. Cheers, -- Cameron Simpson c...@zip.com.au DoD#743 http://www.cskk.ezoshosting.com/cs/ No team manager will tell you this; but they all want to see you come walking back into the pits sometimes, carrying the steering wheel. - Mario Andretti -- http://mail.python.org/mailman/listinfo/python-list
Re: Terminating python script easily
En Thu, 22 Oct 2009 10:03:51 -0300, Bahadir Balban bilgehan.bal...@gmail.com escribió: On Thu, Oct 22, 2009 at 4:01 PM, Jean-Michel Pichavant jeanmic...@sequans.com wrote: Balban wrote: 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? 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. 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: ... -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: Terminating python script easily
En Thu, 22 Oct 2009 10:03:51 -0300, Bahadir Balban bilgehan.bal...@gmail.com escribió: On Thu, Oct 22, 2009 at 4:01 PM, Jean-Michel Pichavant jeanmic...@sequans.com wrote: Balban wrote: 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? 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. 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: ... -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list