Unix Information - Process Management

by D.W. Hyatt

Working With Processes

Each program that runs on a UNIX system is run with a unique process identification number. In this section we will look at how to identify the processes that are running, how to start and stop them, and how to run them in the foreground or background.

Process Status Command   -   ps

To find a list of all processes running on a system, type in the process status command with appropriate options. If you really want to see what was running when I was making this page, you may click on the link to see the output of the command.
ps   -aux

Full Output


To see only those processes that are running by the user dhyatt, send the output of ps through grep. This one is not too long, so this can be right on the page.
ps   -aux   |   grep   dhyatt

dhyatt   11750  0.0  1.1  2792 1520 ?        S    Nov05   0:00 afterstep
dhyatt   11768  0.0  0.6  2012  820 ?        S    Nov05   0:00 /usr/local/bin/An
dhyatt   11769  0.0  0.9  2548 1212 ?        S    Nov05   0:00 /usr/local/bin/Wh
dhyatt   11773  0.0  0.9  2372 1200 ?        S    Nov05   0:00 xeyes
dhyatt   11774  0.0  0.7  2484 1012 ?        S    Nov05   0:00 /usr/local/bin/Pa
dhyatt   11775  0.0  0.7  2456  980 ?        S    Nov05   0:00 /usr/local/bin/Wi
dhyatt   11779  0.0  0.7  1732  992 pts/0    S    Nov05   0:00 bash
dhyatt   11848  0.0  0.6  1496  780 pts/0    S    Nov05   0:00 vi unix01.html
dhyatt   11851  0.0  0.7  1732  992 pts/1    S    Nov05   0:00 bash
dhyatt   11871  0.0  0.5  2368  732 pts/1    R    Nov05   0:00 ps -aux
dhyatt   11872  0.0  0.3  1124  404 pts/1    S    Nov05   0:00 grep dhyatt


To kill a process, just type the command followed by the appropriate Process ID Number. For instance, to kill the program called "xeyes" that creates a pair of eyes that look at the cursor on the screen, just type the kill command with the highest priority level ( -9 ) followed by the process number for that program, 11773.

kill   -9   11773



Foreground and Background Processes

Most commands that UNIX users type will be run from the a console window in the forground. The ps -aux command is typical of one such command. There are times, however, that users do not want to tie up the console window while the command finishes its action. An example might be to run a web browser such as Netscape. These commands may be run in the background by typing an ampersand (&) after the name of the command. This will run the Netscape Browser in the background, returning the window to active use again.

netscape   &


If a process was run in the forground by mistake, it is possible to suspend the process and run it in the background instead. This is done by typing Ctrl-z, the control key and the letter z at the same time. At that point the process is suspended, and the user may type bg to put the current process in the background. It is interesting to note that Ctrl-c terminates the current process, but Ctrl-z suspends the process. If the user wishes to bring a background process to the foreground, just type fg instead.



Job Numbers

A topic related to background process is the concept of job numbers. Since UNIX windows are able to spawn many background processes, each window references them with sequential integers in addition to the process numbers. To see jobs spawned from the current window I am using, I typed jobs.

jobs
[1]   Running                 netscape &
[2]-  Running                 xeyes &
[3]+  Running                 xterm &

The kill command also works with these job numbers, but the job numbers are referenced with the percent sign ( % ). The command kill % terminates the most recent process, or it is also possible to reference a specific process in the job list by number. To kill the copy of "xeyes" started in this window, I could also type:

kill   %2