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.

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 logged into multiple terminals, as the following code shows:

% ps
  PID  TT  STAT      TIME COMMAND
  310 std  S      0:00.37 -tcsh (tcsh)
  510 std  R+     0:00.00 ps
  459  p2  S+     0:00.25 -tcsh (tcsh)
% tty
/dev/ttyp1

std corresponds to your current Terminal window, and p2 corresponds to the Terminal window for ttyp2. In its basic form, ps lists the following:

Process ID (PID)

A unique number assigned by Unix to the process.

Terminal name (TT)

The Unix name for the terminal from which the process was started.

Run time state (STAT)

The current state of each job. S is sleeping, R is runnable, T is stopped, and I is idle (sleeping for more than 20-30 seconds). Additionally, the state can include + to indicate it’s part of the foreground group process, E to indicate the process is exiting, and W to mean it’s swapped out.[12]

Run time (TIME)

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

COMMAND

The name of the process.

Each terminal window has its own terminal name. The previous code shows processes running on two windows: std and p2. If you want to see the processes that a certain user is running, type ps -U username, where username is the username of someone logged into the system.

To see all processes running on the system, use ps -ax. The -a option shows processes from all users, and the -x option shows processes that are not connected with a Terminal session; many of these are processes that are a core part of Mac OS X, while others may be graphical programs you are running, such as a web browser.

You can also specify process ID values to ps to find out about specific jobs. Consider the following:

% sort verybigfile > big-sorted-output
[1]  522 
% ps 522
  PID  TT  STAT      TIME COMMAND
  522 std  R      0:00.32 sort verybigfile
% ps $$
  PID  TT  STAT      TIME COMMAND
  310 std  S      0:00.41 -tcsh (tcsh)

As the last command shows, you can easily ascertain what command shell you’re running at any time by using the $$ shortcut for the process ID of the current shell. Feed that to ps, and it’ll tell you about the shell process you’re running.

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 and have been compiled into a binary format that the system can execute 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 currently.

Shells with job control have a command called jobs that 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 10.1.



[12] The ps manpage has details on all possible states for a process. It’s quite interesting reading.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.142.128