Shell scripting: Print output to logfile, error to logfile & screen
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Red Hat Enterprise Linux, Debian & Ubuntu
Posts: 92
Rep:
Shell scripting: Print output to logfile, error to logfile & screen
Hey Gang,
I'm looking for a KSH, Bourne or Bash way which will do the following:
- Print the standard output of a command to a logfile
- Print the error of the command to the logfile (So I can see the error in it's context) and to the screen (So I can see the error as it happens).
The unix command 'tee' will let me print content to both STDOUT and to a file, but it doesn't deal with STDERR.
This command will print STDOUT and STDERR to a logfile, and will tell me if there was an error:
Code:
make > make.log 2>&1 || print "There was an error"
But I'm looking for a way to print the actual error to the screen. I guess I'm looking for a 'tee'-like program that can deal with STDERR.
Anyone know how do this with a shell script, unix command, or any other method?
"I'm looking for a KSH, Bourne or Bash way which will do the following:
- Print the standard output of a command to a logfile
- Print the error of the command to the logfile (So I can see the error in it's context) and to the screen (So I can see the error as it happens)."
This is an interesting problem. After searching around a bit I managed to print stderr to the screen as well as stderr together with stdout to a log file. However, the context was lost completely; either the stderr messages were printed on the bottom of the log file, or on top of it.
The solutions to similar problems have been about swapping stderr to stdout so that one is able to tee the stderr (which are now at stdout...). I have found a few people asking the same question as yours elsewhere, but those answering have posted solutions to a similar problem only... The best attempt at a solution is CASE 6 here, but it's not very nice...
I can really see the use of this, so I'll keep looking and trying
Hm, I just noticed that stderr in the example on the website I posted above does ALSO loose its context. Not good. Odd that this (is|seems to be) so difficult.
Distribution: Red Hat Enterprise Linux, Debian & Ubuntu
Posts: 92
Original Poster
Rep:
Yeah, isn't it wierd? I've never been able to do this, yet it seems like it would be a very desired feature. I often have commands which spit out thousands of lines of good output which I don't want to see. Burried in the output are some errors... I want to see those errors as they occur (To determine if I want to ignore the error or stop the process), but I also want to see the errors in context, next to the commands that produced the error.
'rsync --ignore-errors' and 'make --keep-going' being two examples that I see alot.
I think I also need to come up with a clearer example.
Interestingly, when I try this in ZSH, I got different output. The STDOUT & STDERR are printed to the screen and to the file 'error'. The file 'output' only contains the output, no error... odd.
I don't know much about zsh, but the above works as follows:
1) The command generates the two streams (output and error streams).
2) File descriptor 2 (default points to /dev/stderr) is redirected to the same file as file descriptor 1 (by default points to /dev/stdout).
3) File descriptor 1 is redirected to append to the file, "output".
4) data on /dev/stdout (from descriptor 2) is piped to tee.
5) tee prints the stream to the console and (by using the -a option) appends to the file, "output".
Not sure how well this will work with a large sample. I don't know if there is any kind of timing issue with the pipeline.
Originally posted by carl.waldbieser I think this will do what you want in bash:
Code:
$ python test.py 2>&1 1>> output | tee -a output
I don't know much about zsh, but the above works as follows:
1) The command generates the two streams (output and error streams).
2) File descriptor 2 (default points to /dev/stderr) is redirected to the same file as file descriptor 1 (by default points to /dev/stdout).
3) File descriptor 1 is redirected to append to the file, "output".
4) data on /dev/stdout (from descriptor 2) is piped to tee.
5) tee prints the stream to the console and (by using the -a option) appends to the file, "output".
Not sure how well this will work with a large sample. I don't know if there is any kind of timing issue with the pipeline.
carl.waldbieser and perfect_circle: Yes, this is exactly what I mean when I say that stderr looses context. The errors are put at the bottom of the log file, not where they occur. That is also the best I've managed myself using only redirection and tee.
Hm, here is one really ugly and stupid solution:
Code:
cmd 2>&1 | tee log | grep -2 ':'
Since most error messages are formatted like cmd: this is an error message, one can grep for ':' and also output some context ("-2"). Of course, if stdout also contains lots of ':' then this doesn't work...
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,788
Rep:
Quote:
Yes, this is exactly what I mean when I say that stderr looses context. The errors are put at the bottom of the log file, not where they occur.
The problem you are complaining of is probably due to buffering / flushing issues.
Stdout and stderr loose their synchronization when piped to another program, so you need a solution not using a pipe. I think that would be easy to solve with Solaris dtrace.
Under Linux, the closest solution would be customizing strace source code to your needs.
Here's a start:
Code:
strace -s 1024 -f -e trace=write -e write=1,2 make
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.