LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 07-11-2013, 10:26 AM   #1
jeriryan
Member
 
Registered: Apr 2003
Location: United States
Distribution: RHEL 5.4, Snow Leopard
Posts: 87

Rep: Reputation: 15
Displaying stdout to screen and file, stderr only to file


Hi guys,

I'm trying to figure out how I can have the stdout from my script print to the screen and a log file, and have stderr print only to the log file. And if possible, have it sequenced correctly (since I've read that because of stdout and stderr being handled by buffers differently, ordering might be rearranged).

Anyone know how I can accomplish this?
 
Old 07-11-2013, 10:51 AM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
What kind of script? If this is a shell script, use redirection and tee (man tee) as follows:
Code:
./foo 2>/tmp/errorlog | tee /tmp/screenlog
edit: this will send STDOUT and STDERR to different files which is not exactly what you asked, but I don't see a button to delete my post.

Last edited by AlucardZero; 07-11-2013 at 10:53 AM.
 
Old 07-11-2013, 10:59 AM   #3
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,147

Rep: Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264Reputation: 1264
The problem is that piping stdout to tee (or anything else) delays stdout so you lose ordering. Combining the outputs preserves order but doesn't let you send them to different places. AlucardZero's solution seems like the best you can do.
 
Old 07-12-2013, 03:18 AM   #4
shm0
Member
 
Registered: Aug 2012
Location: Bahrain
Distribution: Slackware
Posts: 58

Rep: Reputation: 16
Quote:
Originally Posted by jeriryan View Post
Hi guys,

I'm trying to figure out how I can have the stdout from my script print to the screen and a log file, and have stderr print only to the log file. And if possible, have it sequenced correctly (since I've read that because of stdout and stderr being handled by buffers differently, ordering might be rearranged).

Anyone know how I can accomplish this?
You can use the stdbuf utility, here is an example.
 
  


Reply



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
[SOLVED] Print stdout and stderr to screen + log file and catch any errors in shell script idaham Linux - General 3 03-28-2013 06:43 AM
[SOLVED] grep stdout to file and stderr to another file hansfordmc Linux - Newbie 7 08-26-2012 05:47 PM
how to tell if a file is outputing to STDOUT or STDERR? xskycamefalling Linux - Newbie 5 05-15-2009 12:52 AM
send stderr to a file and also to stdout pranavchoudhary Linux - Newbie 7 08-25-2008 01:57 AM
redirecting stdout and stderr to a file Avatar33 Programming 4 03-12-2005 07:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:01 AM.

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