[ Previous | Next | Contents | Glossary | Home | Search ]
AIX Version 4.3 Commands Reference, Volume 1

bg Command

Purpose

Runs jobs in the background.

Syntax

bgJobID ... ]

Description

If job control is enabled (see "Job Control in the Korn Shell" in AIX Version 4.3 System User's Guide: Operating System and Devices), the bg command resumes suspended jobs in the current environment by running them as background jobs. If the specified job is already running in the background, the bg command has no effect and exits successfully. If no JobID parameter is supplied, the bg command uses the most recently suspended job.

The JobID parameter can be a process ID number, or you can use one of the following symbol combinations:

%Number Refers to a job by the job number.
%String Refers to a job whose name begins with the specified string.
%?String Refers to a job whose name contains the specified string.
%+ OR %% Refers to the current job.
%- Refers to the previous job.

Using the bg command to place a job into the background causes the job's process ID to become known in the current shell environment. The bg command output displays the job number and the command associated with that job. The job number can be used with the wait, fg, and kill commands by prefixing the job number with a % (percent sign). For example, kill %3.

A job is suspended by using the Ctrl-Z key sequence. That job can be restarted in the background using the bg command. This is effective if the job expects no terminal input and if job output is redirected to non-terminal files. If a background job has terminal output, the job can be forced to stop by entering the following command:

stty tostop

A background job can be stopped by entering the following command:

kill -s stop JobID

The /usr/bin/bg command does not work when operating in its own command execution environment, because that environment does not have suspended jobs to manipulate. This would be the case in the following example:

Command | xargs bg

Each /usr/bin/bg command operates in a different environment and does not share the parent shell's understanding of jobs. For this reason, the bg command is implemented as a Korn shell or POSIX shell regular built-in.

Exit Status

The following exit values are returned:

0 Successful completion.
>0 An error occurred.

If job control is disabled, the bg command exits with an error, and no job is placed in the background.

Examples

If the output of the job command displays the following stopped job:

[2] +   Stopped (SIGSTOP)   sleep 100 &

use the job number to resume the sleep 100 & command by entering:

bg %2

The screen displays the revised status of job 2:

[2] sleep 100 &

Files

/usr/bin/ksh Contains the Korn shell bg built-in command.
/usr/bin/bg Contains the bg command.

Related Information

The csh command, fg command, jobs command, kill command, wait command.

Job Control in the Korn Shell in AIX Version 4.3 System User's Guide: Operating System and Devices.


[ Previous | Next | Contents | Glossary | Home | Search ]