linuxbashtailfree-command

Bash free command stops working


I am trying to do following:

Get output of free -mo , take the 2nd line and log it to a file every 30 seconds.

When I run

$free -mo -s 30 

It runs and displays output every 30 seconds.

But when I run

$ free -mo -s 30 | head -2 | tail -1

It runs only once. I am not able to figure out what is wrong.

free Manual says free -s 30 run the command every 30 seconds.


Solution

  • head -2 returns only the first 2 lines of output then quits. tail -1 returns the last line, then quits. When any program quits in a pipeline, it kills the entire pipeline, so free is stopped when head and tail finish.