-->
Previous Table of Contents Next


Linux identifies and keeps track of processes by assigning a process ID number (PID) to each process.

Starting Multiple Processes

You’ve already seen that your login shell is always running. Whenever you enter a command, you start at least one new process while the login shell continues to run. If you enter the following command, for example, the file named report.txt is sent to the lp program:


lp report.txt


See “Understanding Shells,” p. 339

When the lp program completes its task, the shell prompt reappears. However, before the shell prompt reappeared, the login shell and the lp command were running; you have initiated multiple processes in that case. The shell waited until the lp command finished before putting the shell prompt back onscreen.

Starting a Background Process

You can run a process as a background job by giving the command to start a process and placing an ampersand (&) after the command. For example, if you enter the command lp report.txt &, the shell responds immediately with a number—the PID for that process. The shell prompt reappears without waiting for the process to complete. The following is a sample of what you would see:


$ lp report.txt &

3146

$

In this example, 3146 is the PID of the process started by the lp command.

Regardless of whether you run the lp command in the background, the process associated with lp is started from the current shell. The lp process is a child process of the current shell. This example points to a common relationship between processes—that of parent and child. Your current shell is the parent process, and the running lp process is a child process. Usually, a parent process waits for one or more of its child processes to complete before it continues. If you want the parent to continue without waiting for the child to finish, attach the ampersand (&) to the command that spawns, or initiates, the child process. You can continue with other work or commands while the child runs.


NOTE:  If you’re working from a character terminal or a remote login, your current shell is usually your login shell. However, if you’re using a virtual terminal or a terminal window from a GUI, a separate shell is associated with each session.

Using Pipes to Start Multiple Processes

Another way to start multiple processes is to use one or more pipes on a command line. To print a long listing of the 10 most recently modified files in your current directory, enter this command:


ls -lt | head | lp

This command starts three processes simultaneously, and they’re all children of the current shell. A pipe works this way: Commands on either side of the vertical bar (|) begin at the same time. Neither is the parent of the other; they’re both children of the process that was running when they were created. In this sense, you can think of commands on either side of the pipe symbol as sibling processes.

Some programs are written so that they themselves spawn several processes. One example is the ispell command, which lists the words in a document that Linux can’t find in a system dictionary. The ispell command spawns some child processes. Suppose you enter this:


ispell final.rept > final.errs &

You’ll see the following results displayed:


1286

$

Here, 1286 is the PID of the ispell process; the $ prompt indicates that the shell is ready to handle another command from you. Even though ispell may spawn some children and wait for them to complete, you don’t have to wait. In this example, the current shell is the parent of ispell, and ispell’s children can be thought of as grandchildren of the login shell. Although a parent can wait for its children, a grandparent doesn’t.

All these examples show how it’s possible for users to start multiple processes. You can wait until child processes are finished before continuing or not. If you continue without waiting for child processes to complete, you make the children background processes. The following section looks at some Linux commands you can use to schedule processes to run at specified times or at a lower relative priority.

Using the Scheduling Commands

The Linux environment provides many ways to handle command execution. Linux lets you create lists of commands and specify when they’re to be run. The at command, for example, takes a list of commands typed at the keyboard or from a file and runs them at the time specified by the command. The batch command is similar to the at command, except that batch runs commands when the system finds time for them rather than allow the user to specify a particular time. The cron command allows for commands to be run periodically, and the crontab command allows users to edit the files used by cron.

All scheduling commands are useful for running tasks at times when the system isn’t too busy. They’re also good for executing scripts to external services—such as database queries—at times when it’s least expensive to do so.

Running Commands at Specified Times with at

To schedule one or more commands for a specified time, use the at command. With this command, you can specify a time, a date, or both. The command expects two or more arguments. At a minimum, you specify the time you want the command(s) executed and the command(s) you want to execute.


Previous Table of Contents Next