2

In Python I want to call a script that prints to the screen as it executes while also logging the stdout and stderr streams. I have the following line of bash code that I'd like to execute:

script.sh > >(tee /tmp/success.log) 2> >(tee /tmp/errors.log >&2)

where script.sh is any script that outputs to both stdout and stderr. This line captures the stdout and stderr streams into the log files success.log and errors.log and also outputs the streams to stdout, as explained here: How do I write stderr to a file while using "tee" with a pipe?

Now I want to execute this from within python using subprocess.call(that_command_as_str, shell=True), but python uses /bin/sh to execute the command instead of /bin/bash, and vanilla sh doesn't support that type of redirection. It seems the usage of /bin/sh is hardcoded into the subprocess module. Any Ideas?

Community
  • 1
  • 1
Cogan
  • 133
  • 1
  • 5

1 Answers1

3

Wrap your command in /bin/bash -c "your-command-stuff".

Then /bin/sh will run bash which will interpret the rest for you. You could also consider using other functions in Python Subprocess to do the redirection, but the above should be quick and effective.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • 1
    Thanks, calling /bin/bash directly works great. I honestly don't think there is a way to easily achieve this functionality using just the subprocess module. – Cogan May 20 '11 at 13:31