Question

I'm trying to log CPU and Memory stats into a file by using top on an Arch Linux. I'm just interested in one specific process and get the wanted parameters as shown below:

top -b -n1 -p 310 | tail -fn 1 | awk '{printf "%s,%s,%s,%s\n",$1,$12,$9,$10}'

This gives me an output to command line like:

310,name,0.0,10.5

So now, if I want to run this command like 10 times with a delay of 1s and write the output to a logfile I use:

top -b -n10 -p 310 -d 1 | tail -fn 1 | awk '{printf "%s,%s,%s,%s\n",$1,$12,$9,$10}' >> log.txt

But, instead printing me line by line to the logfile, I only get the last output. So my logfile contains only 1 line, although top must have been executed 10 times.

What am I doing wrong here?

PS: Printing to command line instead into a logfile produces only 1 line (the last output) as well...

Was it helpful?

Solution

The problem is because of tail command you use. Try something like this

top -p 310-b -n2 -d 1 | grep -w 310 | awk '{printf "%s,%s,%s,%s\n",$1,$12,$9,$10}'

I use grep -w to filter the lines only containing the info you are interested

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top