I 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.
When outputting to the terminal, perl’s output is line buffered, so you will see every line as it is output. When its output is a file, it will be block buffered so you will not see any output until a full block is ready to write (4k I think, but variable and system-defined).
You need to set stdout to use line buffering:
Search for [perl line buffered output] and you’ll see plenty of results about this.