April 24, 2014
 
 
RSSRSS feed

What's Bogging Down Your Linux PC? Tracking Down Resource Hogs - page 2

top Has Many Useful Options

  • July 21, 2009
  • By Akkana Peck

The next workhorse is ps, Process Status. If you type it with no arguments, you'll probably get something that's not very useful:

  PID TTY          TIME CMD
13978 pts/2    00:00:00 bash
15375 pts/2    00:00:00 ps

Add a u (no dash in front of it) to get a lot more information in "user-oriented format":

(imbrium)- ps u
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
akkana    3848  0.0  0.0   4188  2084 tty1     S    08:46   0:00 -tcsh
akkana    3905  0.0  0.0   3504  1552 tty1     S+   08:46   0:00 /bin/bash /usr/
akkana    3965  0.0  0.0   2904   824 tty1     S+   08:46   0:00 xinit /home/akk
akkana    4022  0.0  0.0   1868   504 tty1     S    08:46   0:00 sh /home/akkana
akkana    4032  0.0  0.1   9232  3664 tty1     S    08:46   0:00 xterm -geometry
akkana    4034  0.0  0.0   5572  2840 tty1     S    08:46   0:00 /usr/bin/python
akkana    4038  0.0  0.2  12356  7364 tty1     S    08:46   0:01 openbox
akkana    4040  0.0  0.0   3976  2000 pts/0    Ss   08:46   0:00 -tcsh
akkana    4057  0.0  0.0   3140   716 tty1     S    08:46   0:00 dbus-launch --a
akkana    4065  0.0  0.0   1868   532 tty1     S    08:46   0:00 /bin/sh /usr/lo
akkana    4070  0.0  0.0   1868   548 tty1     S    08:46   0:00 /bin/sh /usr/lo
akkana    4074  0.7  3.1 261620 98948 tty1     Sl   08:46   1:33 /usr/local/fire
akkana    4132  0.0  0.1   8796  3588 tty1     S    08:49   0:00 xterm -geometry
akkana    4133  0.0  0.0   4456  2576 pts/1    Ss+  08:49   0:00 -tcsh
akkana    4598  0.0  0.1   8684  4952 pts/0    S+   08:51   0:00 mutt
akkana    5221  0.0  0.5  26340 17124 tty1     S    09:03   0:05 emacs
akkana   12067  0.0  0.4  27612 13044 tty1     Sl   11:05   0:00 python /home/ak
akkana   13535  0.0  0.6  33096 18624 tty1     S    11:46   0:01 /usr/local/bin/
akkana   13977  0.0  0.1   8788  5472 pts/1    S    11:57   0:00 xterm -bg white
akkana   13978  0.0  0.0   3964  2048 pts/2    Ss   11:57   0:00 -tcsh
akkana   15400  0.0  0.0   2704   976 pts/2    R+   12:24   0:00 ps u

The fields pretty much mirror the ones you see in top, with slightly different names and units.

You may notice that ps u only shows you processes owned by you. When you're trying to speed up your system, don't forget about system processes. Sometimes they can take up a significant amount of time. ps uax will show all processes: the a means "all processes", but despite that, it doesn't really show all processes until you tack on the x to show processes that aren't attached to any terminal -- for instance, processes that were started when your system booted.

One thing you may have noticed about ps output is that it isn't sorted in any useful order. For instance, take this output of ps uax:

(imbrium)- ps uax
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0   3080  1884 ?        Ss   08:45   0:00 /sbin/init
root         2  0.0  0.0      0     0 ?        S<   08:45   0:00 [kthreadd]
root         3  0.0  0.0      0     0 ?        S<   08:45   0:00 [migration/0]
root         4  0.0  0.0      0     0 ?        S<   08:45   0:00 [ksoftirqd/0]
root         5  0.0  0.0      0     0 ?        S<   08:45   0:00 [watchdog/0]
root         9  0.0  0.0      0     0 ?        S<   08:45   0:00 [events/0]
root        11  0.0  0.0      0     0 ?        S<   08:45   0:00 [khelper]
root        17  0.0  0.0      0     0 ?        S<   08:45   0:00 [async/mgr]
root       162  0.0  0.0      0     0 ?        S<   08:45   0:00 [kblockd/0]
root       165  0.0  0.0      0     0 ?        S<   08:45   0:00 [kacpid]
root       166  0.0  0.0      0     0 ?        S<   08:45   0:00 [kacpi_notify]
root       233  0.0  0.0      0     0 ?        S<   08:45   0:00 [ata/0]
root       235  0.0  0.0      0     0 ?        S<   08:45   0:00 [ata_aux]
root       236  0.0  0.0      0     0 ?        S<   08:45   0:00 [ksuspend_usbd]
root       242  0.0  0.0      0     0 ?        S<   08:45   0:00 [khubd]
root       245  0.0  0.0      0     0 ?        S<   08:45   0:00 [kseriod]
[ ... ]
It goes on, but you get the idea: the list is obviously sorted by process ID. It would make a lot more sense to sort by CPU or memory use, especially when you're trying to speed up your system. So you can specify a sort order, like:
ps uax --sort=-rss
to sort by memory use (resident set size). The dash in front of rss tells ps to put the biggest processes first. Other useful sort options are -pcpu, to sort by CPU use and -time to sort by cumulative time the process has ued.

You can also use those specifiers to indicate what fields ps will show, in case you're not interested in details like Status, Start time, Priority and so forth. For the full list of parameters you can use, search for STANDARD FORMAT SPECIFIERS in the ps manual page.

For instance, here's a useful ps command that gives much more useful output (Figure 2):

ps ax --sort=-pcpu o user,pid,pcpu,pmem,vsz,rss,stat,time,comm

figure 2
figure 2

Of course, you wouldn't want to type that every time. So choose a set of arguments that make sense to you, and make a shell alias in your .bashrc file:

alias psall "ps ax --sort=-pcpu o user,pid,pcpu,pmem,vsz,rss,stat,time,comm"

Akkana Peck is a freelance programmer and author of the book Beginning GIMP: From Novice to Professional. She'll be discussing some techniques for speeding up Linux in a talk at OSCON this Wednesday. Her website is is shallowsky.com.


Sitemap | Contact Us