Book HomeLearning UnixSearch this book

7.2. Checking on a Process

If a background process takes too long, or you change your mind and want to stop a process, you can check the status of the process and even cancel it.

7.2.1. ps

When you enter the command ps, you can see how long a process has been running, the process ID of the background process and the terminal from which it was run. The tty program shows the name of the terminal where it's running; this is especially helpful when you're using a window system or you're logged into multiple terminals. Example 7-1 shows this in more detail.

Example 7-1. Output of ps and tty programs

$ ps
   PID TTY      TIME CMD
 27285 pts/3    0:01 csh
 27285 pts/3    0:01 ps
 29771 pts/2    0:00 csh
 29792 pts/2    0:54 sort
$ tty
/dev/pts/3

In its basic form, ps lists the following:

Process ID (PID)
A unique number assigned by Unix to the process.

Terminal name (TTY)
The Unix name for the terminal from which the process was started.

Run time (TIME)
The amount of computer time (in minutes and seconds) that the process has used.

Command (CMD)
The name of the process.

In a window system, each terminal window has its own terminal name. Example 7-1 shows processes running on two terminals: pts/3 and pts/2. Some versions of ps list only the processes on the same terminal where you run ps; other versions list processes on all terminals where you're logged in. If you have more than one terminal window open, but all the entries in the TTY column show the same terminal name, try typing either "ps x" or "ps -u username", where username is your username. If you need to find out the name of a particular terminal, run the tty program from a shell prompt in that window, as shown in Example 7-1.

While using a window system, you may see quite a few processes you don't recognize; they're probably helping the window manager do its job. You may also see the names of any other programs running in the background and the name of your shell's process (sh, csh, and so on)--although different versions of ps may show fewer processes by default. ps may or may not list its own process.

You should be aware that there are two types of programs on Unix systems: directly executable programs and interpreted programs. Directly executable programs are written in a programming language such as C or Pascal and stored in a file that the system can read directly. Interpreted programs, such as shell scripts and Perl scripts, are sequences of commands that are read by an interpreter program. If you execute an interpreted program, you will see an additional command (such as perl, sh, or csh) in the ps listing, as well as any Unix commands that the interpreter is executing now.

Shells with job control have a command called jobs which lists background processes started from that shell. As mentioned earlier, there are commands to change the foreground/background status of jobs. There are other job control commands as well. See the references in Section 8.1 in Chapter 8.



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.