[英]python 2 subprocess check_output not returning error output
I have this method 我有这种方法
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
which work but for some reason doesn't return the error output from a specfici command 哪个工作,但由于某种原因不会返回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')
output: 输出:
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
when I run command ctmpsm -UPDATEAJF 0e4ba HOLD just form the normal shell i get the below error output 当我运行命令ctmpsm -UPDATEAJF 0e4ba HOLD只是形成普通的外壳时,我得到以下错误输出
ctmtest1-tctmsv80 [288] ctmpsm -UPDATEAJF 0e4ba HOLD
Failed to Hold Orderno 0000e4ba. (rc=JOBSTATINCM).
This is different to the un-useful error output in my python code and I can't for the life of me figure out why? 这与我的python代码中无用的错误输出不同,我无法终生弄清楚为什么?
UPDATE: 更新:
Trying stderr=subprocess.STDOUT 尝试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
output: 输出:
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
Now errors have completely disappeared? 现在错误已经完全消失了吗?
As documented , when check_output
raises an exception, it places the output of the command in the output
attribute of the exception object. 如文档所述 ,当check_output
引发异常时,它将命令的output
放置在异常对象的output
属性中。 You can do the following: 您可以执行以下操作:
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
By specifying stderr=subprocess.STDOUT
, you can make the output written to standard error to be redirected to standard output. 通过指定stderr=subprocess.STDOUT
,可以使写入标准错误的输出重定向到标准输出。
p = subprocess.check_output(
string_command,
shell=True,
stderr=subprocess.STDOUT,
env=env_variables)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.