monitor custom programs with ps and watch

March 26th, 2007 mysurface Posted in Admin, ps, watch | Hits: 44708 | 6 Comments »

ps is a very useful tool to list all current running processes with various info such as CPU usage, memory usage, process status, process id etc.

watch is another good tool to continuously execute some programs in infinite loop. watch allows you to make use of commands such as ps, netstat, lsof into monitoring purpose.

The common use of ps is to list all executing processes in user-oriented format.

ps aux

To the extend of checking the “target” processes, let say “fluxbox”, we do this,

ps aux | grep fluxbox

With that, the lines with keyword “fluxbox” will be grepped. But, the first line of ps which use to display the field caption will be lost.

Output looks like this:

mysurface     4491  0.0  1.4  23516  7256 ?        Ss   21:15   0:02 /usr/bin/fluxbox
mysurface     5962  0.0  0.1   2800   768 pts/0    S+   21:51   0:00 grep fluxbox

You probably can remember which field indicate what, but I can’t. I couldn’t remember the value 1.4 is what. Furthermore, the last line, “grep fluxbox”, is not what I targeted.

As long as you know the exactly you want, you can use the command line as bellow:

ps u -C fluxbox

Same way I want it to be user-oriented and I ask ps to give me only my targeted “process”.

USER           PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
mysurface     4491  0.0  1.4  23516  7256 ?        Ss   21:15   0:02 /usr/bin/fluxbox

Neat and nice isn’t it? Thanks to bytee for the tip.

You can place a list of targeted processes. But bare in mind, its not like grep, you have put the exact process name.

ps u -C fluxbox,gaim,xchat

At last, you wanna monitor these command list, that is where watch take place,

watch -n 1 "ps u -C fluxbox,gaim,xchat"

-n is to specify the time interval, execute the line every 1 second. Ctrl+c to quit watch.

6 Responses to “monitor custom programs with ps and watch”

  1. Or how about a better one:
    ps aux | grep [f]luxbox

    Note that you can put the brackets around any one of the letters in the process you want to grep for. That will leave out the grep process just the same without having to remember different ps options.

    The joys of modern shells :)

  2. [...] I have make the comparison between this application and a simple ncurses program, this program seems to use lesser memory. Couldn’t measure for CPU resources, as both ps and top shows 0.0 %. [...]

  3. Good Command. Thanks

  4. [...] But, ps capable of doing more, one of my favorite post, Monitor custom programs with ps and watch [...]

  5. [...] monitor custom programs with ps and watch ps is a very useful tool to list all current running processes with various info such as CPU usage, memory usage, proces… [...]

  6. pgrep will return the pid of a named process.By default, multiple results are listed in a verticle column. You can make that more “ps friendly” by setting a comma as a delimiter ( the -d switch).
    Note that the examples use backticks, not single quotes.
    ps u -p `pgrep -d, tty`
    Or use the same idea in
    top p `pgrep -d, tty`
    You can get the same result with
    top p $(pgrep -d, tty)

Leave a Reply