Softpanorama
(slightly skeptical) Open Source Software Educational Society

May the source be with you, but remember the KISS principle ;-)

Google   


Unix ps command and Unix system tuning

News

See Also

Books

Recommended Links      
nice top vmstat time x11perf Humor Etc
(System V)

Common options:

Meaning of full listing columns:

(BSD)

Common options:

Meaning of user information columns:

Old News

Server Oriented System Tuning Info

Okay, so everyone knows about ps. But I'll just highlight one of my favorite options:

 

ps -eo pid,%cpu,vsz,args,wchan

Shows every process, their pid, % of cpu, memory size, name, and what syscall they are currently executing. Nifty.

redhat.com Home P.S.—I want to see my processes

One of the most basic tools we can use is the utility ps. ps provides a snapshot of current processes. This snapshot can range from myself as a single user (such as what active processes I have running) to all the processes on the system. The simple example of course is to run the ps command with no options, which produces output similar to:

  PID TTY          TIME CMD
 2873 pts/1    00:00:00 bash
 3002 pts/1    00:00:00 ps
Example 1. Basic output of ps

We see in Example 1, “Basic output of ps” that we get some minimal information about the processes we are running, including ps itself. ps displays the process ID (PID), the terminal associated with the process (TTY), the cumulated CPU time in [dd-]hh:mm:ss format (TIME), and the executable name (CMD). Spectacular, right? Well, ps does this and a whole lot more. I should mention at this point that the version of ps that I am using for this article is something special compared to the ps of yester-year and of your classic UNIX®. This ps, procps version 3.2.5, accepts several kinds of options: UNIX options, which may be grouped and must be preceded by a dash, BSD options, which may be grouped and must not be used with a dash, and GNU long options, which are preceded by two dashes. For the uninitiated, those who are new to Linux, or refugees from some older BSD or System V variant, this is good news. A system administrator can track down a process via several sets of options.

root      2784  2774  0 22:45 pts/2    00:00:00 su - mfrye
mfrye     2785  2784  0 22:45 pts/2    00:00:00 -bash
root      2895  1870  0 23:04 ?        00:00:00 sshd: mfrye [priv]
mfrye     2897  2895  0 23:04 ?        00:00:00 sshd: mfrye@pts/3
mfrye     2898  2897  0 23:04 pts/3    00:00:00 -bash
mfrye     3274  2785  0 23:34 pts/2    00:00:00 ps -ef
mfrye     3275  2785  0 23:34 pts/2    00:00:00 grep mfrye
Example 2. Output of ps -ef | grep mfrye
root      2784  0.0  0.0  71368  1288 pts/2    S    22:45   0:00 su - mfrye
mfrye     2785  0.0  0.0  55124  1536 pts/2    S    22:45   0:00 -bash
root      2895  0.0  0.1  38228  2660 ?        Ss   23:04   0:00 sshd: mfrye [priv]
mfrye     2897  0.0  0.1  38228  2748 ?        S    23:04   0:00 sshd: mfrye@pts/3
mfrye     2898  0.0  0.0  55124  1528 pts/3    Ss   23:04   0:00 -bash
mfrye     3272  0.0  0.0  52948   872 pts/2    R+   23:34   0:00 ps aux
mfrye     3273  0.0  0.0  51192   636 pts/2    S+   23:34   0:00 grep mfrye
Example 3. Output of ps -aux | grep mfrye

In Example 2, “Output of ps -ef | grep mfrye” and Example 3, “Output of ps -aux | grep mfrye”, we see the output of ps with different arguments. We can use this output to track a particular set of processes (owned by mfrye) via either of two sets of options (UNIX & BSD, respectively). So what's the big deal, you're thinking? OK, so bash is a pretty tame example. In cases where another process, perhaps one that consumes more memory, or some other resource, than you want, ps can be a very quick, easy, and effective way to track that process down. So now we've tracked down a particular process, but we don't know much more than some basic information about the process's CPU usage in terms of accumulated CPU time, which as you may appreciate, is not ideal. Luckily, there's more.

Killing (Numerous) Processes By S. Lee Henry

Undoubtedly, you are familiar with the kill command, which sends a signal (usually a TERM) to a running process. The default signal, if you don't specify one on the command line, will stop most processes from running. The kill command is an easy command that most Unix users learn, as well as kill -9 (also called the "sure kill"), long before they understand the exact nature of the kill command (i.e., sending a signal which a process will read and process) and its difference from simply stopping a process -- one of many options, though the most common.

When you want to kill a process using a script and need to determine the process ID, or when you want to kill a number of processes without having to use both the ps and kill command for each of them, you run into complications. This column presents a code snippet for killing a number of processes "by name". We use the Bourne shell (this will work with other shells in the Bourne shell family as well) and the xargs command to compose a kill command on the fly.

Here's the code:

------------------------------ cut here ------------------------------
#!/bin/sh

procs2kill="proc1 proc2 proc3 proc4 proc5"

for proc in `echo $procs2kill`
do
  ps -ef | grep $proc | grep -v grep | awk '{print $2}' | xargs -i kill
{}
  $proc killed
done
------------------------------ cut here ------------------------------
The procs2kill variable should define the processes to be killed as precisely as possible -- include as much of a substring as possible so that you will select the proper processes and only the proper processes. If you are overly generous in your specification (e.g., you specify a string that is too general and will match more than a single process), then you might kill more processes than you intended. Once established, the procs2kill variable is then used in the "for" loop to kill each process. The grep -v command removes any sign of the grep command in the process table; the awk command reduces the ps output to the process id column; and the xarg command then creates a kill command with each process id.

The Bourne shell "for proc in " command is very similar to the C Shell's foreach command -- foreach PROC (echo $procs2kill). The syntax is very similar and each command assigns each value, in turn, to the proc variable.

Recommended Links


In case of broken links please try to use Google search. If you find the page please notify us about new location
Google     

docs.sun.com man pages section 1 User Commands ps man page

Suse performance tuning



Copyright © 1996-2008 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

Standard disclaimer: The statements, views and opinions presented on this web page are those of the author and are not endorsed by, nor do they necessarily reflect, the opinions of the author present and former employers, SDNP or any other organization the author may be associated with. We do not warrant the correctness of the information provided or its fitness for any purpose.

Last modified: June 05, 2008