How to capture the output of a top command in a file in linux?
Here is the 1-liner I like to use on my mac:
top -o -pid -l 1 | grep "some regexp"
Cheers.
How about using while
loop and -n 1
:
while sleep 3; do
top -b -n1 | grep init > top-output.txt
done
for me top -b > test.txt
will store all output from top
ok even if i break it with ctrl-c
. I suggest you dump first, and then grep
the resulting file.
It looks like the output is not writing to the file until all iterations are finished. You could solve this by wrapping with an external loop like this:
touch top-output.txt
while true; do
top -b | grep init >> top-output.txt
done