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.

have the following command line that outputs vmstat every second with a time stamp on each line via the perl script:

vmstat 15 | /home/Beer/addtimestamp.pl > File_1 

the contents of the addtimestamp.pl:

!/usr/bin/perl while (<>) { print localtime() . ": $_"; }

So why doesn't the output get redirected to the "File_1" file?

It works perfectly when I don't, it prints out the output perfectly every second with no issues at all.

share|improve this question
    
There's a utility to do this: ts, part of the moreutils package. –  donothingsuccessfully Dec 11 '12 at 7:51
add comment

1 Answer

up vote 3 down vote accepted

You fell into the buffering Gotcha. Perl buffers its output and only writes out to disk when the buffer is full. Buffering is a good thing performance-wise, but at low data rates can be confusing. If you wait long enough you'll notice your file being written (check with tail -F File_1). I believe standard buffer is 4kB in size.

Add $|=1 like below to disable buffering in Perl:

vmstat 1 | perl -e '$| = 1; while (<>) { print localtime() . ": $_"; }' > /tmp/fileetje
share|improve this answer
    
I just add '$|=1' to the start of the script and it worked like a Charm! Wait, that's actually correct, why am I using two scripts when I can just merge both!, Awesome Dude! Thanks! –  I AM L Dec 11 '12 at 8:03
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.