Why is subprocess.Popen returncode different for similar commands using bash

Why

import subprocess

p = subprocess.Popen(["/bin/bash", "-c", "timeout -s KILL 1 sleep 5 2>/dev/null"])
p.wait()
print(p.returncode)

returns

[stderr:] /bin/bash: line 1: 963663 Killed                  timeout -s KILL 1 sleep 5 2> /dev/null
[stdout:] 137

when

import subprocess

p = subprocess.Popen(["/bin/bash", "-c", "timeout -s KILL 1 sleep 5"])
p.wait()
print(p.returncode)

returns

[stdout:] -9

If you change bash to a dash, you get 137 in both cases. I know that -9 is the KILL code, and 137 is 128 + 9. But for such a code, it seems strange to get a different return code.

Happens on Python 2.7.12 and python 3.4.3

It Popen.wait()does not seem to call Popen._handle_exitstatus https://github.com/python/cpython/blob/3.4/Lib/subprocess.py#L1468 when used /bin/bash, but I could not understand why.

+6
source share
1 answer

, bash timeout / bash:

    • python bash
    • bash timeout, .
    • timeout sleep
    • timeout SIGKILL
    • , , bash timeout, SIGKILL , , stderr. 128 + 9 (, timeout).
    • python bash.
    • bash , execve(), timeout.
    • timeout , , SIGKILL.
    • python 9 , -9 (SIGKILL)

, // .. bash . : subprocess.Popen() bash, . bash , timeout , .

, timeout --foreground; 124 .

; , execve(), . , 128% - .

: zsh , , timeout -s KILL 1 sleep 5 >/tmp/foo , -9. timeout -s KILL 1 sleep 5 && echo $? 137 zsh.

+4

All Articles