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?

link|improve this question
feedback

2 Answers

up vote 1 down vote accepted

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
link|improve this answer
This gave me the output file I wanted. Thank you! – Mark Jan 16 at 19:54
feedback

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.

link|improve this answer
Thanks for the tip! – Mark Jan 16 at 23:28
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.