Tuesday, December 16, 2008

Redirect top command output to file - Linux

By default, top is invoked using interactive mode in which top runs indefinitely and accepts keypress to redefine how top works.

But, in case one need to post-process the top's output and this is hardly achieved using this interactive mode. For this we need to use top's batch mode.

from man pages of top:

-b : Batch mode operation
Starts top in 'Batch mode', which could be useful for sending output from top to other programs or to a file. In this mode, top will not accept input and
runs until the iterations limit you've set with the '-n' command-line option or until killed.

So this is how we can redirect top output to a file.

$ top -b -n 1 > top.out.$(date +%s)

Related post:
- Linux htop command - an interactive process viewer for Linux


Pavan Devarakonda [PD] said...

Hi I wan to know how many threads spawned for a java process (WebLogic Server instance). In prsat -L -p pid 1 1 gives it. I am looking similar kind of option in Linux. Can you reply ASAP.

Unknown said...


Not sure if this will help.

ps -eLf | grep "your_process"

-L Show threads, possibly with LWP and NLWP columns

© Jadu Saikia www.UNIXCL.com