awk buffers its output. If your awk implementation provides it (as gawk, mawk1, nawk, and BSD awk do), use fflush().
fflush([file]) Flush any buffers associated with the open output file
or pipe file. If file is missing or if it is the null
string, then flush all open output files and pipes.
So, write it this way:
vmstat 1 | awk '{print $2 > "outfile"; fflush()}'
The GNU awk manual I/O section on fflush is a worth reading. There you will also find that fflush has been accepted to the next POSIX standard.
As an extra, notice that you can give the number of samples that vmstat should output. So, if you want only 5 samples (for example), you can wait the 5 seconds until the command terminates and then the file will contain the output:
vmstat 1 5 | awk '{print $2 > "outfile"}'
1With mawk the syntax is a bit different: mawk -W interactive '{print $2 > "outfile"; fflush("")}'.
-W interactiveoption help?-W interactiveis supported bymawk.