Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm trying to hack in better logging for a test program that I didn't write. I want all lines of STDERR output to have timestamps, but if possible I want to leave STDOUT alone. I also want one file (all.txt) to log both stdout and stderr (either with or without STDERR timestamps), and a file dedicated to STDERR output (errors.txt, with timestamps)

Also, I don't care whether anything is echoed to the terminal or not. This is a batch-style program.

I did look at rsyslog and that might come in later, but this is a solution I should be able to hack in real quick...at least, if I knew more about output redirection. I read a bunch of similar questions but apparently nothing with this exact situation.

I think in order to add timestamps to stderr output, I want to use a pipe and the technique from this question: http://serverfault.com/questions/310098/adding-a-timestamp-to-bash-script-log

What is the cleanest solution here? Note that I have RHEL5 systems that are on bash 3.2.25.

            +----stdout-------------------+
           /                               \
test.pl --+                                 +--> all.txt
           \                               /
            +----stderr | add_timestamps -+----> errors.txt
share|improve this question
    
To add timestamps you could try ts from the package moreutils. –  donothingsuccessfully Aug 20 '12 at 18:14
add comment

1 Answer

up vote 4 down vote accepted

I think this will do what you want:

test.pl 2>&1 >all.txt | add_timestamps | tee -a all.txt > errors.txt

  • Redirect stderr to stdout (so we can pipe it later)
  • Redirect (the original) stdout to all.txt
  • Pipe stdout (which now contains the error messages) to add_timestamps
  • Use tee to append errors (with timestamps) to all.txt
  • Finally, also write the errors to errors.txt
share|improve this answer
1  
I think there's a possibility of a race-condition here. Both tee and stdout of test.pl are writing to all.txt. There's no guarantee that the lines will be in the same order that would be seen when running it in a terminal. –  donothingsuccessfully Aug 20 '12 at 18:13
1  
Right, but since they are separate streams I'm not sure if there is any way to prevent that –  lk- Aug 20 '12 at 18:17
    
Right you are. A the possibility of a race condition seems unavoidable what ever the solution. –  donothingsuccessfully Aug 20 '12 at 18:24
1  
    
This solution worked fine for me, thanks. –  twblamer Aug 21 '12 at 21:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.