I'm launching a program with subprocess
on Python.
In some cases the program may freeze. This is out of my control. The only thing I can do from the command line it is launched from is CtrlEsc which kills the program quickly.
Is there any way to emulate this with subprocess
? I am using subprocess.Popen(cmd, shell=True)
to launch the program.
Well, there are a couple of methods on the object returned by
subprocess.Popen()
which may be of use:Popen.terminate()
andPopen.kill()
, which send aSIGTERM
andSIGKILL
respectively.For example...
...would terminate the process after five seconds.
Or you can use
os.kill()
to send other signals, likeSIGINT
to simulate CTRL-C, with...Your question is not too clear, but If I assume that you are about to launch a process wich goes to zombie and you want to be able to control that in some state of your script. If this in the case, I propose you the following:
This in not really recommanded to pass through the shell. I would suggest you ti use shell=False, this way you risk less an overflow.
You can use two signals to kill a running subprocess call i.e., signal.SIGTERM and signal.SIGKILL; for example
Check out the docs on the
subprocess
module for more info: http://docs.python.org/2/library/subprocess.html