Get `top` output for non-interactive shell

scriptingtop

I want to implement top function for a web app. Is it possible to get the output of the top command in non-interactive shell?

Best Answer

top -b -n 1

From man 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.
Related Question