“subprocess.Popen” – checking for success and errors

Do you need to do anything with the output of the process? The check_call method might be useful here. See the python docs here: https://docs.python.org/2/library/subprocess.html#subprocess.check_call You can then use this as follows: try: subprocess.check_call(command) except subprocess.CalledProcessError: # There was an error – command exited with non-zero code However, this relies on command returning an exit … Read more

Redirect subprocess stderr to stdout

In Python < v3.5: A close read of the source code gives the answer. In particular, the documentation is misleading when it says: subprocess.STDOUT Special value that (…) indicates that standard error should go into the same handle as standard output. Since stdout is set to “default” (-1, technically) when stderr=subprocess.STDOUT is evaluated, stderr is … Read more

Capture both stdout and stderr in Bash [duplicate]

There’s a really ugly way to capture stderr and stdout in two separate variables without temporary files (if you like plumbing), using process substitution, source, and declare appropriately. I’ll call your command banana. You can mimic such a command with a function: banana() { echo “banana to stdout” echo >&2 “banana to stderr” } I’ll … Read more

Getting STDOUT, STDERR, and response code from external *nix command in perl

This was exactly the challenge that David Golden faced when he wrote Capture::Tiny. I think it will help you do exactly what you need. Basic example: #!/usr/bin/env perl use strict; use warnings; use Capture::Tiny ‘capture’; my ($stdout, $stderr, $return) = capture { system( ‘echo Hello’ ); }; print “STDOUT: $stdout\n”; print “STDERR: $stderr\n”; print “Return: … Read more

How do I temporarily redirect stderr in Ruby?

In Ruby, $stderr refers to the output stream that is currently used as stderr, whereas STDERR is the default stderr stream. It is easy to temporarily assign a different output stream to $stderr. require “stringio” def capture_stderr # The output stream must be an IO-like object. In this case we capture it in # an … Read more

How to replicate tee behavior in Python when using subprocess?

I see that this is a rather old post but just in case someone is still searching for a way to do this: proc = subprocess.Popen([“ping”, “localhost”], stdout=subprocess.PIPE, stderr=subprocess.PIPE) with open(“logfile.txt”, “w”) as log_file: while proc.poll() is None: line = proc.stderr.readline() if line: print “err: ” + line.strip() log_file.write(line) line = proc.stdout.readline() if line: print … Read more

Capture program stdout and stderr to separate variables

One option is to combine the output of stdout and stderr into a single stream, then filter. Data from stdout will be strings, while stderr produces System.Management.Automation.ErrorRecord objects. $allOutput = & myprogram.exe 2>&1 $stderr = $allOutput | ?{ $_ -is [System.Management.Automation.ErrorRecord] } $stdout = $allOutput | ?{ $_ -isnot [System.Management.Automation.ErrorRecord] }

Redirect stdout+stderr on a C# Windows service

You can do this via PInvoke to SetStdHandle: [DllImport(“Kernel32.dll”, SetLastError = true) ] public static extern int SetStdHandle(int device, IntPtr handle); // in your service, dispose on shutdown.. FileStream filestream; StreamWriter streamwriter; void Redirect() { int status; IntPtr handle; filestream = new FileStream(“logfile.txt”, FileMode.Create); streamwriter = new StreamWriter(filestream); streamwriter.AutoFlush = true; Console.SetOut(streamwriter); Console.SetError(streamwriter); handle = … Read more