22

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.)

3 Answers 3

32

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
            killed.
....
-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.
5
  • 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, 2014 at 16:35
  • @Mickalot: See my updated. Can you install GNU top in Mac OSX?
    – cuonglm
    Jul 30, 2014 at 16:48
  • 1
    @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, 2014 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, 2014 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, 2014 at 21:46
1

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.

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

macos hack

Sorry, this is horribly hacky, but it got me what I wanted on macos:

top -l 2 -n 10 | tail -22
  • -l 2 - as per comment macos will output 0 in the CPU field if given a one-only sample size. So, sample twice.

  • -n 10 take only top process (by CPU load is the default order)

  • tail -22 - the real hackish part of this - skip the lines concerning the first sample, -22 is totally by trial and error in this case and is directly related to -n 10.

Proud of this? By no means, but it got me roughly what I wanted.

Processes: 547 total, 3 running, 544 sleeping, 2558 threads 
2022/11/24 17:18:44
Load Avg: 1.74, 2.05, 2.01 
CPU usage: 6.25% user, 1.71% sys, 92.2% idle 
SharedLibs: 257M resident, 35M data, 14M linkedit.
MemRegions: 229362 total, 3865M resident, 114M private, 1192M shared.
PhysMem: 16G used (3357M wired), 143M unused.
VM: 11T vsize, 2317M framework vsize, 11336543(0) swapins, 13536939(0) swapouts.
Networks: packets: 33586023/7445M in, 34143918/6707M out.
Disks: 9964939/225G read, 3781186/116G written.

PID    COMMAND          %CPU TIME     #TH    #WQ #PORTS MEM    PURG CMPRS PGRP  PPID  STATE    BOOSTS   %CPU_ME %CPU_OTHRS UID FAULTS    COW   MSGSENT    MSGRECV    SYSBSD     SYSMACH    CSW        PAGEINS IDLEW     POWER INSTRS     CYCLES     USER          #MREGS RPRVT VPRVT VSIZE KPRVT KSHRD
17013  postgres         28.5 01:00.77 1      0   8      62M+   0B   0B    17013 16225 sleeping *0[1]    0.00000 0.00000    501 545151+   124   152        5          353119+    118842+    11731+     0       0         28.5  3760288663 1617021064 postgres      N/A    N/A   N/A   N/A   N/A   N/A  
17019  postgres         25.9 01:04.16 1/1    0   8      64M+   0B   0B    17019 16225 running  *0[1]    0.00000 0.00000    501 552890+   123   160+       5          359735+    120494+    11930+     0       0         25.9  3263636650 1446362124 postgres      N/A    N/A   N/A   N/A   N/A   N/A  
17329  top              8.8  00:00.52 1/1    0   28+    3728K+ 0B   0B    17329 13212 running  *0[1]    0.03698 0.00000    0   2471+     106+  517539+    255643+    4594+      264177+    48+        0       0         8.8   388290277  451214497  root          N/A    N/A   N/A   N/A   N/A   N/A  
16245  Python           8.3  08:43.72 1      0   21     1027M+ 0B   1104K 16245 37017 sleeping *0[1]    0.00000 0.00000    505 1008120+  3429  3229       41         8420748+   6791       2448946+   87      35        8.3   442524899  451503337  me__          N/A    N/A   N/A   N/A   N/A   N/A  
17015  postgres         5.1  00:12.19 1      0   8      29M+   0B   0B    17015 16225 sleeping *0[1]    0.00000 0.00000    501 8276+     114   14         5          360531+    30         85127+     0       5         5.1   516698298  269517335  postgres      N/A    N/A   N/A   N/A   N/A   N/A  
17020  postgres         4.8  00:12.64 1      0   8      32M+   0B   0B    17020 16225 sleeping *0[1]    0.00000 0.00000    501 9074+     113   14         5          400661+    34         94522+     0       6         4.8   486455789  262738456  postgres      N/A    N/A   N/A   N/A   N/A   N/A  
0      kernel_task      3.7  01:46:28 287/16 0   0      1436M  0B   0B    0     0     running   0[0]    0.00000 0.00000    0   6106435   9589  331617464+ 300334081+ 0          0          615407045+ 183     32498664+ 0.0   96723302   461786276  root          N/A    N/A   N/A   N/A   N/A   N/A  
15976  Python           3.0  01:39.90 2      0   24     90M    0B   4860K 94785 94785 sleeping *0[1]    0.00000 0.00000    505 26811     643   460        21         6928600+   1008       28526+     448     1886+     3.1   235167151  177692860  me__          N/A    N/A   N/A   N/A   N/A   N/A  
16226  postgres         1.6  00:39.95 1      0   8      728K   0B   188K  16226 16225 sleeping *0[1]    0.00000 0.00000    501 340       73    23         5          10634810+  20         2383516+   0       4         1.6   37954507   77088931   postgres      N/A    N/A   N/A   N/A   N/A   N/A  
150    WindowServer     1.4  04:23:51 16     7   2944   1307M- 19M  107M  150   1     sleeping *0[1]    0.02412 0.01154    88  15718938+ 72229 255258396+ 99186014+  280197394+ 409489438+ 118640818+ 3284    3028879   1.4   29648136   64463377   _windowserver N/A    N/A   N/A   N/A   N/A   N/A  

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .