Process Output Logging in Linux and Windows

As I was updating this Wikia page, I added a section about how to create a simple logfile. I thought it’d be useful to expand upon the subject here.

Sometimes you want a raw log of whatever happens during a process, whether it’s coming in via STDERR or STDOUT (usually so you can see the warning or error in the context of the rest of the output). A little redirection and judicious use of “tee” will accomplish this. You may not know you wanted a log at all until you spent 5 hours running a process and an error occurred 20,000 lines back. Here’s a quick and easy way to log that output on the off-chance you’ll need it later!

Simple logging for bash (Linux or Cygwin) or a Windows command prompt (assumes you have “tee” installed somewhere in your path, e.g., via Cygwin). The “2>&1” part redirects STDERR to STDOUT prior to piping STDOUT to “tee”:

MyUtil -arg1 A -arg2 B 2>&1 | tee mylogfile.log

Simple logging for csh/tcsh (Linux or Cygwin). The |& forces STDERR to be piped to “tee” alone with the usual STDOUT:

MyUtil -arg1 A -arg2 B      |& tee mylogfile.log

Note: non-interactive processes are far more amenable to this… if manual input is expected you probably won’t see the prompt (since tee won’t flush its buffer until EOF or a newline, at a minimum (it may buffer MANY lines before displaying them). If you know when it’ll occur you CAN enter the data at the appropriate time (or press return or what have you). This is a quick and dirty way to log something you’d rather not dig into to instrument.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.