Search code examples
pythonpython-2.7subprocesspopenexit-code

Python subprocess.Popen return code after killing with signal


Consider the following code:

SIGNAL = 123
proc = subprocess.Popen("longrunning")
proc.send_signal(SIGNAL)

Now what I want is to get the exit code that has been returned from the code with exit().

Unfortunately, in Python I get only the signal number that the process was stopped with:

>>> proc.returncode == -SIGNAL
True

Solution

  • The two are mutually exclusive. If you process is terminated as a result of getting a signal it won't get a chance to produce a return code with exit().

    See man 2 waitpid for explanation on the underlying system call mechanism.