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

时间:2015-07-08 06:20:11

标签: python python-2.7 subprocess

我有这个方法

Benchmark.bm do |b|
 b.report('joined') do
   1000.times do
     ActiveRecord::Base.connection.execute('
       select
          p.id,
          (select count(c.id) from comments c where c.post_id = p.id) comment_count
       from
          posts l;')
   end
 end

 b.report('nested') do
   1000.times do
     ActiveRecord::Base.connection.execute('
       select
          p.id,
          count(c.id) comment_count
       from
          posts File.join(File.dirname(__FILE__), *%w[rel path here])

          left join comments c on
            c.post_id = p.id
       group by
          p.id;')
   end
 end
end

       user     system      total        real
nested  2.120000   0.900000   3.020000 (  3.349015)
joined  2.110000   0.990000   3.100000 (  3.402986)

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

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

输出:

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')

当我运行命令ctmpsm -UPDATEAJF 0e4ba HOLD刚刚形成普通shell我得到以下错误输出

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

这与我的python代码中无用的错误输出不同,我不能为我的生活找出原因?

更新:

尝试stderr = subprocess.STDOUT

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

输出:

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

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

2 个答案:

答案 0 :(得分:5)

作为documented,当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

答案 1 :(得分:0)

通过指定stderr=subprocess.STDOUT,可以将写入标准错误的输出重定向到标准输出。

p = subprocess.check_output(
    string_command,
    shell=True,
    stderr=subprocess.STDOUT,
    env=env_variables)