How to redirect output of top command in a file How to redirect output of top command in a file unix unix

How to redirect output of top command in a file


top -b -n 1 seems to work on my Linux box here (-b: batch mode operation, -n: number of iterations).

Edit:

I just tried it on FreeBSD 9.2 which uses the 3.5beta12 version of top. It seems it needs at least one additional iteration to get CPU stats. So you might want to use:

top -b -d2 -s1 | sed -e '1,/USERNAME/d' | sed -e '1,/^$/d'

-b: batch mode, -d2: 2 displays (the first one does not contain CPU stats, second one does), -s1: wait one seconds between displays

The sed pipeline removes the first display which does not contain CPU stats (by skipping header and process list).