Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 10-11-2012, 08:42 AM   #1
LQ Newbie
Registered: Oct 2012
Posts: 17

Rep: Reputation: Disabled
how to show output and error on the screen and put them to log file

I want to run a program which will show output and error on screen
I also want to put them into file while being shown on the screen
I tried tee but the results are always not as expected
Old 10-11-2012, 09:13 AM   #2
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 705

Rep: Reputation: 425Reputation: 425Reputation: 425Reputation: 425Reputation: 425

You should redirect standard error to standard output before using tee. For example in bash:
<program> 2>&1 | tee logfile
In recent versions of bash there is a shorthand for this operation:
<program> |& tee logfile

Last edited by firstfire; 10-11-2012 at 09:16 AM.
Old 10-11-2012, 09:30 AM   #3
Senior Member
Registered: Aug 2009
Distribution: CentOS
Posts: 3,545

Rep: Reputation: 1558Reputation: 1558Reputation: 1558Reputation: 1558Reputation: 1558Reputation: 1558Reputation: 1558Reputation: 1558Reputation: 1558Reputation: 1558Reputation: 1558
Without more information on what you have already tried, it's hard to give advice. Here's what I would try first:
some_program arg1 arg2 2>&1 | tee some_file
Output buffering can be an issue when a program's output that would normally go to a terminal is redirected somewhere else, such as to a pipe. Output that is going to a terminal is, by default, line buffered, whereas output (other than stderr) not going to a terminal is block buffered (typically 4KB blocks). That can result in messages and regular output not appearing in the same sequence they would when everything is going directly to a terminal. Workarounds for that depend on what program you are running, and include:
  1. In a C program, calling setbuf(3) to override the default buffering behavior,
  2. Using a specific flag argument to override buffering in programs that have that feature (e.g., the "--line-buffered" option in grep),
  3. Using the unbuffer utility (part of the expect package.
Old 10-12-2012, 04:51 AM   #4
Senior Member
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 13.1
Posts: 1,328

Rep: Reputation: 254Reputation: 254Reputation: 254
Originally Posted by esolve View Post
I tried tee but the results are always not as expected
What do you mean by:„…but the results are always not as expected“?

What do you see on screen and what is in the file?


output, tee

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Clean Up Log - Search for Pattern in Log file and Output result bridrod Linux - Newbie 10 01-05-2010 09:49 AM
How do i log GNU screen output to a file ? RadishTP Linux - Newbie 3 06-12-2009 02:35 PM
Log in screen won-t show up, I get a black screen and mouse pinter on waiting mode Nis2k Linux - General 1 03-26-2008 09:32 PM
Can I show different output on the laptop screen and the external one? riwaJR Linux - Laptop and Netbook 9 11-14-2006 02:01 PM
Log Screen Output to a file sdandeker Linux - Newbie 3 09-17-2003 02:57 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:13 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration