I am trying to write a command that pipes the continuous output of a free command (run every second) to an awk command that parses a specific value (available free memory) and outputs this to a file with a timestamp. Here are my current attempts at the command:

free -mto -s 1 | awk '/Mem/ { print strftime("%r") "," $4 }' >>memOut

And alternatively, after a bit of Googling

free -mto -s 1 | awk '/Mem/ { print strftime("%r") "," $4 >>"memOut"}'

Each run produces empty files. Any suggestions or possibly different methods?

4 Answers 4


You have to flush the buffer to see something in memOut during the execution:

free -mto -s 1 | awk '/Mem/ { print strftime("%r") "," $4; fflush(stdout) }' >> memOut

Here's an alternative version:

while sleep 1; do sed -n "s/MemFree: */`date`, /p" /proc/meminfo; done >> memOut
  • This gave me the output file I wanted. Thank you!
    – Mark
    Commented Jan 16, 2012 at 19:54

For old versions of awk you may have to use system("").

Actually, fflush(stdout) is for only for recent versions of awk and gawk, as its only in the POSIX standard since December 2012.

free -mto -s 1 | awk '/Mem/ { 
    print strftime("%r") "," $4;
    system(""); # Flush output
}' >> memOut

Note that using system("") flushes every file descriptor, its full description is in the gawk manual, section "9.1.4 Input/Output Functions".


On certain versions of awk (e.g. mawk 1.3.3) you need to add the -W interactive command line flag to enable unbuffered operation with pipes.


Just to make sure that you are getting what you actually want and not what you specifically asked.

If you want to know available memory on the system for programs then this might be more suitable:

free -m -s 1 | awk '/buffers\/cache/ { print strftime("%r") "," $4; fflush(stdout) }' >> memOut

Mem line's Used column includes caches and buffers and in most cases when you want to monitor memory usage for given computer/task those should at least be noted.


You must log in to answer this question.

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