Capturing process output (STDOUT, STDERR) via input, output redirection

Last edited on

Synopsis

To capture all output of a process/program (for example squishserver and squishrunner) the I/O redirection functionality commonly found in "shell" programs (bash and others on Unix systems, cmd.exe on Windows) can be used.

For example:

myprogram >stdout.txt 2>stderr.txt
Execute this in a shell/cmd.exe

Merging STDOUT and STDERR into a single file

To merge the STDOUT and STDERR channels and redirect them into a single file:

myprogram >stdout.txt 2>&1

In this, the order is often relevant, so the "2>&1" part should appear at the end.

Executing commands in/with a new console window (Windows)

To start a process in a new, separate console window - and still capture its output - cmd.exe must be used together with the "start" (command which is built into cmd.exe):

start cmd.exe /c "squishserver --verbose >squishserver_output.txt 2>&1"

Note that the parameters for the new process (squishserver in the above example) needs to be passed as a single parameter to cmd.exe and be enclosed in double quotes.

Redirecting all Output of a Command or Batch File

For this one can use a little helper batch file: capture_output.bat

USAGE:

capture_output.bat command_to_execute [output_file_name_optional]

Example:

capture_output.bat execute_squish_tests.bat execute_squish_tests_output.txt

Note the second (optional) parameter which specifies the to path, and name of the desired output file. When not specified the output file name will be the "command_to_execute" with ".txt" appended to it.