Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99

Previous Table of Contents Next


Waiting for Background Processes to Finish (wait Command)

There are two ways to wait for a background process to finish before doing something else. You can press the Enter key every few minutes until you get the completion message, or you can use the wait command.

There are three ways to use the wait command—with no options (the default), with a process ID, or with a job number prefixed with a percent sign. The command will wait for the completion of the job or process you specify.

If you do not specify a job or process (the default setting), the wait command waits for all background jobs to finish. Using wait without any options is useful in a shell script that starts a series of background jobs. When they are all done, it can continue processing.

With the ls command from the previous example running, I can force a wait with the following:

$ wait %1

I can not enter another command until job number 1 finishes. If I use wait, I do not get the completion message (Done).

Listing Running Processes

You can start processes in the foreground and background, suspend them, and move them between the foreground and background, but how do you know what is running? There are two commands to help you find out—jobs and ps.

jobs Command

The jobs command shows you the processes you have suspended and the ones running in the background. Because the jobs command is a foreground process, it cannot show you your active foreground processes.

In the following example, I have three jobs. The first one (job 3) is running, the second (job 2) is suspended (a foreground process after I used Ctrl+Z), and the third one (job 1) is stopped in the background to wait for keyboard input:

$ jobs
[3] +  Running                 first_one &
[2] - Stopped (SIGTSTP)        second_one
[1]   Stopped (SIGTTIN)        third_one &

I can manipulate these jobs with the fg and bg commands. The most recent job is job number 3 (shown with a plus sign); this is the one that bg or fg act on if no job number is supplied. The most recent job before that is job number two (shown with a minus sign).


Note:  
The reason for the plus and minus symbols on the jobs listing is that job numbers are reassigned when one completes and another starts. In the previous example, if job number 2 finishes and you start another job, it is assigned job number 2 and a plus sign because it is the most recent job.

ps Command

Another command that shows all processes running is the ps (Process Status) command. By default, it shows those processes that you are running. It also accepts many different options, a few of which are shown here.

There are different flavors, or versions, of UNIX. ps is one command where the differences are very obvious. The examples in this chapter are based on System V, a UNIX standard developed by UNIX Systems Labs (USL) when it was part of AT&T. If you are using a version of UNIX based on Berkeley Systems Division (BSD), like Linux, your output will be different.

The simplest example (with the same three jobs running as the previous example) is the ps command alone:

$ ps
   PID TTY      TIME CMD
  6738 pts/6    0:00 first_one
  6739 pts/6    0:00 second_one
  3662 pts/6    0:00 ksh
  8062 pts/6    0:00 ps
  6770 pts/6    0:01 third_one

For each running process, this provides me with four pieces of information: the pid, the TTY (terminal running this process), the Time or amount of CPU consumed by this process, and the command name running.

I am running three jobs but have five processes. Of course, one of the extra processes is the ps command itself. The remaining process, ksh, is the command shell that interprets what I type at the keyboard and manages the processes and jobs. It is my interface to the operating system and nothing would happen without it. ksh is the Korn shell.


Note:  
If you are using a version of UNIX based on BSD, like Linux, your output is different and looks like the following:
$ ps
  PID  TT  STAT      TIME COMMAND
13049  q0  Ss     0:00.06 -ksh (ksh)
13108  q0  R+     0:00.01 ps

For each running process, this provides you with five pieces of information: the pid, the TT (terminal running this process), STAT (the state of the job), the TIME or amount of CPU consumed by this process, and finally the command name running.

Use the man ps command for an explanation of the states and options available.



Note:  
In some versions of UNIX, the ps command does not show itself in the listing. That’s just the way it works. I used a version that does show it. If you don’t see it on your system, don’t worry.

One of the most commonly used flags for ps is the -f (f for full) option, which provides more information as shown in the following example:

$ ps -f
     UID   PID  PPID  C    STIME TTY      TIME CMD
 dhorvath 6738  3662  0 10:23:03 pts/6    0:00 first_one
 dhorvath 6739  3662  0 10:22:54 pts/6    0:00 second_one
 dhorvath 3662  3657  0 08:10:53 pts/6    0:00 -ksh
 dhorvath 6892  3662  4 10:51:50 pts/6    0:00 ps -f
 dhorvath 6770  3662  2 10:35:45 pts/6    0:03 third_one

Table 6.1 shows the meaning of each of these columns.

Table 6.1 ps -f Columns

Column Heading Description

UID User ID that this process belongs to (the person running it).
PID Process ID.
PPID Parent process ID (the ID of the process that started it).
C CPU utilization of process.
unlabeled Nice value—used in calculating process priority.
STIME Process start time (when it began).
CMD The command that started this process. CMD with -f is different from CMD without it; it shows any command line options and arguments.


Previous Table of Contents Next