Output to a log file AND the screen

4th Jan 2010 // By Chris // Systems

It's great to keep logs of things to go back and find out what happened, but sometimes it's also useful to have the information there on the screen so you can see what's going on at the time. I discovered a nice way of doing this using tee which I thought I'd share:

echo -e "First line\nSecond line" | tee /path/to/log.file

echo -e "Third line" | tee -a /path/to/log.file

The first line creates the file, then the second (and then subsequent lines) use the -a switch to append to the file. It's a nice easy way of making sure that your output goes to two places at once. Of course, you could use any command at the start of the line, not just echo.

Image by Dmitry Baranovskiy.

About The Author

Chris's Profile Picture
Chris

Chris Cohen is a seasoned web developer who began in Perl, before moving on to Java, C# and currently PHP and Drupal. He regularly attends DrupalCon and is "Certified to Rock" (certifiedtorock.com).

0

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.

New job vacancies button

Categories

Search