I find the output of the shell command top to be a simple and familiar way to get a rough idea of the health of a machine. I'd like to serve top's output (or something very similar to it) from a tiny web server on a machine for crude monitoring purposes.

Is there a way to get top to write its textual output exactly once, without formatting characters? I've tried this:

(sleep 1; echo 'q') | top > output.txt

This seems to be close to what I want, except that (1) there's no guarantee that I won't get more or less than one screenful of info and (2) I have to strip out all the terminal formatting characters.

Or is there some other top-like command that lists both machine-wide and process-level memory/CPU usage/uptime info?

(Ideally, I'd love a strategy that's portable to both Linux and Mac OS X, since our devs use Macs and our prod environment is Linux.)


In Linux, you can try this:

top -bn1 > output.txt

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
-n : Number-of-iterations limit as:  -n number
            Specifies  the  maximum  number of iterations, or frames, top
            should produce before ending.

With OS X, try:

top -l 1

From top OSX manpage:

 -l <samples>
              Use logging mode and display <samples> samples, even if 
              standard output is a terminal. 0 is treated  as  infinity.   
              Rather than redisplaying, output is periodically printed in 
              raw form. Note that the first sample displayed will have an 
              invalid %CPU displayed for each process,  as it is calculated 
              using the delta between samples.
  • This looks like exactly the answer I would be looking for if top on OS X supported the -b flag. :-( Sadly, it doesn't, and I don't see a version of top available through homebrew. This seems like exactly the right answer for Linux, tho. – Mickalot Jul 30 '14 at 16:35
  • @Mickalot: See my updated. Can you install GNU top in Mac OSX? – cuonglm Jul 30 '14 at 16:48
  • @Gnuoc: /usr/bin/top -l 1 on OSX 10.9.4 is perfect...thanks! As for installing GNU top, I could install it on my machine, but I'd have to convince the other devs to install it on their machines for my code to work for them, so I'd rather avoid that. (If it's not available through homebrew, there will be resistance.) Now that I'll be switching based on platform, I'm going to be greedy...are you aware of any Windows semi-equivalent so those devs (we have a few) can also have a crude status page? (If not, that's fine, your answer is already super-helpful!) – Mickalot Jul 30 '14 at 17:27
  • @Mickalot: Remember the note from OSX top manpage, Note that the first sample displayed will have an invalid %CPU displayed for each process, as it is calculated using the delta between samples.. With Windows users, maybe you can use ``cygwin` – cuonglm Jul 30 '14 at 17:32
  • @Gnuoc Thanks for the extra point. I suppose I can do top -l 2 and throw away the first page of samples? – Mickalot Jul 30 '14 at 21:46

To get similar type numbers from a Windows system you will want to take a look at powershell.

Just to get a list of processes you and look at get-process. Take a look at this reference.

In doing some further searches, found a nice little command here.

Which if you take out of the while loop presented, for your needs would be:

ps | sort -desc cpu | select -first 30

ps in powershell is an alias for get-process.

  • That sounds like a great idea! I'll get one of my Windows-toting coworkers to try that out... – Mickalot Jul 30 '14 at 21:46

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.