12

I want to implement top function for the web app, is it possible to get the output of top command in non interactive shell.

  • Does it have to have the same format as top? – Raphael Ahrens Jan 13 '16 at 14:14
  • @RaphaelAhrens is there an alternative? – jcubic Jan 13 '16 at 14:14
  • Let me rephrase What information do you want to have? Processes and there CPU usage? Load? Uptime? Number of tasks? – Raphael Ahrens Jan 13 '16 at 14:16
  • @RaphaelAhrens I wanted to have everything that top have. – jcubic Jan 13 '16 at 14:18
15

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.
  • 3
    also consider top -n1 – Jeff Schaller Jan 13 '16 at 15:11
  • so if i want to add a column in non interactive mode. say i want to add swap usage by process how do i do that ? usually in interactive mode this is what i do : shift + f ----> press p ---> hit enter. – Sagar Jun 21 '17 at 13:49
  • For completeness, answering Sagar's question: When running top in interactive mode you can choose columns and orderings, then press "W" to save $HOME/.toprc . This file will be read in next calls to "top", be interactive or not. Simply remove that file to return to defaults. Regards, /Angel – Angel Dec 1 '17 at 11:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.