How to redirect values from top command to a file in Mac OSX

3

All, I want to extract values from top for plotting purposes. I'm currently using the following command

top -l 0 -s 1  -pid 12345 -stats rsize | awk 'NR%13==0'  

When I run this , I get the expected output. But when I try to redirect the output to a file by running the following:

top -l 0 -s 1  -pid 299 -stats rsize | awk 'NR%13==0' > output.txt  

it doesn't work. What should I do to redirect the output to a file?

I'm on a Mac OSX(Lion)

smokinguns

Posted 2012-02-19T07:14:57.967

Reputation: 1 188

1It seems that batch mode (-b) is not implemented on OS X. You could set larger value for -l (say "-l 10"), but you won't be able to see the output until after the script is done, e.g. "tail -f output.txt" won't work for monitoring. – lupincho – 2012-02-19T07:45:04.923

@lupincho: the only problem is awk buffering. – tuergeist – 2012-02-19T21:32:04.153

Answers

2

Your expression is rigth, but be aware that awk buffers its output. So, adding fflush(stdout) to your awk part will help.

top -l 0 -s 1  -pid 299 -stats rsize | awk 'NR%13==0; fflush(stdout)' > output.txt  

tuergeist

Posted 2012-02-19T07:14:57.967

Reputation: 300