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.


1 Answer 1


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
  • 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, 2012 at 8:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .