bg Command

Purpose

Runs jobs in the background.

Syntax

bgJobID ... ]

Description

If job control is enabled (see "Job Control in the Korn shell or POSIX shell" in AIX® Version 7.1 Operating system and device management), 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:

Item Description
%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:

Item Description
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 jobs command displays the following stopped job:
[2] + Stopped (SIGSTOP)  sleep 100 &

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

bg %2

The screen displays the revised status of job 2:

[2] sleep 100 &

Files

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