Previous Section
 < Day Day Up > 
Next Section


Jobs: Background, Kills, and Interruptions

In Linux, you not only have control over a command's input and output, but also over its execution. You can run a job in the background while you execute other commands. You can also cancel commands before they have finished executing. You can even interrupt a command, starting it again later from where you left off. Background operations are particularly useful for long jobs. Instead of waiting at the terminal until a command has finished execution, you can place it in the background. You can then continue executing other Linux commands. You can, for example, edit a file while other files are printing. The background commands, as well as commands to cancel and interrupt jobs, are listed in Table 8-5.

Table 8-5: Job Management Operations

Background Jobs

Execution

&

Execute a command in the background.

fg %jobnum

Bring a command in the background to the foreground or resume an interrupted program.

bg

Place a command in the foreground into the background.

CTRL-Z

Interrupt and stop the currently running program. The program remains stopped and waiting in the background for you to resume it.

notify %jobnum

Notify you when a job ends.

kill %jobnum
kill processnum

Cancel and end a job running in the background.

jobs

List all background jobs. The jobs command is not available in the Bourne shell, unless it is using the jsh shell.

ps

List all currently running processes, including background jobs.

at time date

Execute commands at a specified time and date. The time can be entered with hours and minutes and qualified as A.M. or P.M.

Running Jobs in the Background

You execute a command in the background by placing an ampersand on the command line at the end of the command. When you do so, a user job number and a system process number are displayed. The user job number, placed in brackets, is the number by which the user references the job. The system process number is the number by which the system identifies the job. In the next example, the command to print the file mydata is placed in the background:

$ lpr mydata &
[1]  534
$

You can place more than one command in the background. Each is classified as a job and given a name and a job number. The command jobs lists the jobs being run in the background. Each entry in the list consists of the job number in brackets, whether it is stopped or running, and the name of the job. The + sign indicates the job currently being processed, and the - sign indicates the next job to be executed. In the next example, two commands have been placed in the background. The jobs command then lists those jobs, showing which one is currently being executed.

$ lpr intro &
[1]  547
$ cat *.c > myprogs &
[2]  548
$ jobs
[1]  +  Running  lpr intro
[2]  -  Running  cat *.c > myprogs
$

Job Notification

After you execute any command in Linux, the system tells you what background jobs, if you have any running, have been completed so far. The system does not interrupt any operation, such as editing, to notify you about a completed job. If you want to be notified immediately when a certain job ends, no matter what you are doing on the system, you can use the notify command to instruct the system to tell you. The notify command takes a job number as its argument. When that job is finished, the system interrupts what you are doing to notify you the job has ended. The next example tells the system to notify the user when job 2 has finished:

$ notify %2

Bringing Jobs to the Foreground

You can bring a job out of the background with the foreground command, fg. If only one job is in the background, the fg command alone will bring it to the foreground. If more than one job is in the background, you must use the job's number with the command. You place the job number after the fg command, preceded with a percent sign. A bg command also places a job in the background. This command is usually used for interrupted jobs. In the next example, the second job is brought back into the foreground. You may not immediately receive a prompt again because the second command is now in the foreground and executing. When the command is finished executing, the prompt appears and you can execute another command.

$ fg %2
cat *.c > myprogs
$

Stopping and Suspending Jobs

If you want to stop a job running in the background, you can force it to end with the kill command. The kill command takes as its argument either the user job number or the system process number. The user job number must be preceded by a percent sign, %. You can find out the job number from the jobs command. In the next example, the jobs command lists the background jobs; then job 2 is canceled:

$ jobs
[1]  +  Running  lpr intro
[2]  -  Running  cat *.c > myprogs
$ kill %2

You can also cancel a job using the system process number, which you can obtain with the ps command. The ps command displays a great deal more information than the jobs command does. The next example lists the processes a user is running. The PID is the system process number, also known as the process ID. TTY is the terminal identifier. The time is how long the process has taken so far. COMMAND is the name of the process.

$ ps
PID     TTY       TIME      COMMAND
523     tty24     0:05      sh
567     tty24     0:01      lpr
570     tty24     0:00      ps

You can then reference the system process number in a kill command. Use the process number without any preceding percent sign. The next example kills process 567:

$ kill 567

You can suspend a job and stop it with the CTRL-Z key. This places the job to the side until it is restarted. The job is not ended; it merely remains suspended until you want to continue. When you're ready, you can continue with the job in either the foreground or the background using the fg or bg command. The fg command restarts a suspended job in the foreground. The bg command places the suspended job in the background.

At times, you may need to place a currently running job in the foreground into the background. However, you cannot move a currently running job directly into the background. You first need to suspend it with CTRL-Z, and then place it in the background with the bg command. In the next example, the current command to list and redirect .c files is first suspended with CTRL-Z. Then that job is placed in the background.

$ cat *.c > myprogs
^Z
$ bg


Previous Section
 < Day Day Up > 
Next Section
This HTML Help has been published using the chm2web software.