2

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

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

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?

UPDATE:

Trying 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?

1
  • unrelated: cmd is unused in your code. Commented Jul 8, 2015 at 19:24

2 Answers 2

5

As documented, when check_output raises an exception, it places the output of the command in the output attribute of the exception object. 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 
Sign up to request clarification or add additional context in comments.

Comments

0

By specifying stderr=subprocess.STDOUT, you can make the output written to standard error to be redirected to standard output.

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

3 Comments

thanks for the reply falsetru. I have tried that and it seems to remove all my output: see updated in post
@JustinS, Try print(p) after the subprocess.check_output call.
@JustinS, If you just want to let the program output to terminal instead of catching the output, use subprocess.call: subprocess.call(string_command, shell=True, env=env_variables)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.