python 2子进程check_output没有返回错误输出

Jus*_*n S 2 python subprocess python-2.7

我有这个方法

def do_sh_shell_command(string_command, env_variables=None):
    cmd = shlex.split(string_command)
    try:
       p = subprocess.check_output(string_command, shell=True,
                                   env=env_variables) # shell=True means sh shell used 
    except subprocess.CalledProcessError as e:
        print 'Error running command: ' + '"' + e.cmd + '"' + ' see above shell error'
        print 'Return code: ' + str(e.returncode)
        return e.returncode, e.cmd
    return 0, p
Run Code Online (Sandbox Code Playgroud)

哪个工作但由于某种原因不返回specfici命令的错误输出

def hold_ajf_job(job_order_id):
    #print 'ctmpsm -UPDATEAJF ' + job_order_id + ' HOLD'
    return do_sh_shell_command('ctmpsm -UPDATEAJF ' + job_order_id + ' HOLD')

hold_ajf_job('0e4ba')
do_sh_shell_command('lsl')
Run Code Online (Sandbox Code Playgroud)

输出:

ctmpsm -UPDATEAJF 0e4ba HOLD
Error running command: "ctmpsm -UPDATEAJF 0e4ba HOLD" see above shell error
Return code: 1
/bin/sh: lsl:  not found
Error running command: "lsl" see above shell error
Return code: 127
Run Code Online (Sandbox Code Playgroud)

当我运行命令ctmpsm -UPDATEAJF 0e4ba HOLD只是形成正常的shell我得到以下错误输出

ctmtest1-tctmsv80 [288] ctmpsm -UPDATEAJF 0e4ba HOLD
Failed to Hold Orderno 0000e4ba. (rc=JOBSTATINCM).
Run Code Online (Sandbox Code Playgroud)

这与我的python代码中无用的错误输出不同,我不能为我的生活弄清楚为什么?

更新:

尝试stderr = subprocess.STDOUT

def do_sh_shell_command(string_command, env_variables=None):
    cmd = shlex.split(string_command)
    try:
       p = subprocess.check_output(string_command, stderr=subprocess.STDOUT, shell=True,
                                   env=env_variables) # shell=True means sh shell used 
    except subprocess.CalledProcessError as e:
        print 'Error running command: ' + '"' + e.cmd + '"' + ' see above shell error'
        print 'Return code: ' + str(e.returncode)
        return e.returncode, e.cmd
    return 0, p
Run Code Online (Sandbox Code Playgroud)

输出:

Error running command: "ctmpsm -UPDATEAJF 0e4ba HOLD" see above shell error
Return code: 1
Error running command: "lsl" see above shell error
Return code: 127
Run Code Online (Sandbox Code Playgroud)

现在错误已经完全消失了?

use*_*342 5

文档所述,当check_output引发异常时,它会将命令的输出output放在异常对象的属性中.您可以执行以下操作:

try:
    p = subprocess.check_output(string_command, stderr=subprocess.STDOUT,
                                shell=True, env=env_variables)
except subprocess.CalledProcessError as e:
    print e.output
    print 'Error running command: ' + '"' + e.cmd + '"' + ' see above shell error'
    print 'Return code: ' + str(e.returncode)
    return e.returncode, e.cmd
return 0, p
Run Code Online (Sandbox Code Playgroud)