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

c popen won’t catch stderr

popen gives you a file handle on a process’ stdout, not its stderr. Its first argument is interpreted as a shell command, so you can do redirections in it: FILE *p = popen(“prog 2>&1”, “r”); or, if you don’t want the stdout at all, FILE *p = popen(“prog 2>&1 >/dev/null”, “r”); (Any other file besides … Read more

How to redirect stderr in Python?

I have a piece of software I wrote for work that captures stderr to a file like so: import sys sys.stderr = open(‘C:\\err.txt’, ‘w’) so it’s definitely possible. I believe your problem is that you are creating two instances of writer. Maybe something more like: import sys class writer(object): log = [] def write(self, data): … Read more

How do I print to stderr in Python?

I found this to be the only one short, flexible, portable and readable: import sys def eprint(*args, **kwargs): print(*args, file=sys.stderr, **kwargs) The optional function eprint saves some repetition. It can be used in the same way as the standard print function: >>> print(“Test”) Test >>> eprint(“Test”) Test >>> eprint(“foo”, “bar”, “baz”, sep=”—“) foo—bar—baz

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

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 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] }