How to Use the bg Command in Linux

How to Use the bg Command in Linux

Terminal work sometimes means you need a command to keep running while you return to the shell prompt. The bg command resumes a stopped shell job in the background, returning control of the prompt.

I’ll walk through resuming a stopped job with bg and checking its state.

TL;DR

The bg command resumes a stopped job under shell job control, usually after you suspend a foreground command with Ctrl+Z.

  • Run jobs to see the shell’s job numbers and states.
  • Use bg %1 to resume job 1 in the background.
  • Run jobs again to check that the job is running.
  • Use fg %1 when the job needs to interact with the terminal again.

What Does the bg Command Do?

A shell job is a command or pipeline tracked by the shell. The bg builtin resumes a suspended job selected by a jobspec.

bg [jobspec]

In Bash, %1 selects job 1, %% or %+ selects the current job, and %- selects the previous job.

JobspecMeaning in Bash
%1Job number 1
%% or %+Current job
%-Previous job
%?textJob whose command line contains text, if the match is unambiguous

I checked the installed Bash built-in help. It confirms the bg syntax and jobs filters described here.

Bash marks its current job with a plus sign and its previous job with a minus sign in jobs output. Those markers can change as jobs change state.

The long jobs listing adds process IDs without replacing the shell’s job number. This is useful when a process monitor shows several processes for a single pipeline. Use the process IDs with process-oriented tools, and use the shell’s selector with bg.

How to Resume a Stopped Job with bg

Run bg where the job was stopped. A harmless waiting command is useful for learning because it has no effect beyond occupying time until it finishes.

Step 1: Start a command and suspend it

In an interactive terminal, start a harmless waiting command such as sleep 300. Press Ctrl+Z while it is in the foreground. The shell suspends the foreground job and prints a status line with its job number.

See the sleep command guide for its timing behavior.
sleep 300
# Press Ctrl+Z after the command starts

The text after the hash is an instruction, not a command to type. The shell prompt returns after the job stops, and that prompt is where you enter the next command.

Step 2: Find the job number

Use jobs to inspect the stopped entry before selecting it.

jobs

Bash marks each entry with a bracketed job number and a state such as Stopped. Use the number after the percent sign to select the listed job.

Step 3: Resume the selected job

Pass the job number to bg to continue that stopped job in the background.

bg %1

Bash prints the resumed job and command, then returns control to the prompt while that command continues.

Step 4: Check the job state

Run jobs again to confirm the state changed. You can filter the listing to running jobs with jobs -r or to stopped jobs with jobs -s in Bash.

jobs
jobs -r
jobs -s
jobs -l

The visible output is from Bash. The sleep command itself prints nothing:

[1]+  Stopped                 sleep 300
[1]+ sleep 300 &
[1]+  Running                 sleep 300 &

The first line records the stopped state. The next line is bg’s confirmation, and the final line is the following jobs check. The command name stays attached to the job in each notification.

These are shell-state messages rather than progress output from the program. The stopped-to-running change is what matters in this example. It confirms that the shell resumed the job and kept it in its own job table.

Bash showing a stopped sleep job resumed with bg
The shell lists the stopped sleep job, then shows it running after bg.

If the resumed command finishes quickly, it may disappear from the active job list before you check. In that case, Bash has already reported its completion.

What Should You Check Before Using bg?

The right choice depends on why the command stopped and what the process needs while it runs.

SituationWhat to do
Job is stopped in this interactive shellUse jobs, then run bg with the selected jobspec
Job should continue in the foregroundUse fg with its jobspec
Command has not started yetStart it with & if background execution is appropriate
Command must survive closing the terminalUse a session manager or a service designed for that purpose. bg alone does not detach the job

The ampersand and bg also act at different points. sleep 300 & starts a new asynchronous job, while bg %1 resumes an existing stopped job. For tasks that should continue after logout, see how nohup changes a command’s hangup behavior.

A non-interactive script generally has no interactive job table to manage with bg. If Bash reports that job control is disabled, run the operation at an interactive prompt instead.

Some shells warn when active jobs remain at exit, but the warning is not a persistence mechanism.

Use the Linux job-control guide for the broader relationship between shell jobs and terminal control. When you need to inspect processes beyond the current shell’s jobs, use the ps command. When the goal is to stop a process rather than resume it, see how killall stops processes by name.

Bash reports an error when a requested job does not appear in the active listing. Check the command and shell before trying another selector.

When bg prints a job number and command, it confirms which job the shell resumed. This line does not report the command’s eventual exit status.

Stopped means a job is suspended and can be continued. Running means it is executing in the foreground or background.

For a background command launched by a shell script, wait can collect its exit status after completion.

Use a supervisor for a long-running service.

While a foreground command is stopped, other shell commands can run. After resumption, its output can arrive at the prompt while you are doing other work, so redirect it to a log when interleaved messages would be confusing.

Linux sends SIGTTIN when a background process reads from its controlling terminal. A process that does not handle that signal stops, so use fg when it needs terminal input.

POSIX also specifies that asking bg to resume a job that is already running in the background has no effect and succeeds. Check the current job state before diagnosing a no-op as a failed command.

A background command can still use processor time while it runs. Use nice when the task is to set scheduling priority.

Keyboard-generated signals also follow the terminal’s foreground process group. Ctrl+C is sent to the foreground group, so a job running in the background will not receive that keystroke as if it were in front. Choose fg when the command needs direct terminal control again, or use the appropriate process-control command for a separate termination task.

Bash accepts more than one jobspec after bg, so a single invocation can resume several suspended jobs. That is handy when you deliberately stopped a group of shell jobs and want them all to continue. Keep the selectors explicit, since an error in one selection can make it harder to see which job you meant to resume.

bg %1 %2

Run jobs in the same shell immediately before choosing a target.

POSIX specifies that bg has no effect when the selected job is already running in the background, and the utility still succeeds.

Conclusion: Resume the Job in Its Shell

Use the Bash and POSIX references below for the exact behavior supported by your shell.

For the exact Bash behavior, consult the Bash job-control built-ins reference and the Bash job-control overview. The POSIX specification for bg explains the portable utility behavior and its job-control requirements.

FAQ

These answers cover common choices after a command stops or returns to the prompt.

How do I run bg on the current job?

Run bg without a jobspec in the interactive shell that owns the job. Bash uses its current-job selection. Use jobs first when you want to confirm which job is current.

Can bg resume a process by PID?

No. bg takes a shell jobspec, such as %1, rather than an arbitrary process ID. Use the shell’s jobs command to find jobs it knows about.

Does bg keep a command running after I close the terminal?

Not by itself. bg resumes a job in the shell’s job-control context. Use a suitable session manager or service when a task must continue independently of that terminal session.

How do I bring a background job back to the prompt?

Use fg with the job’s jobspec, for example fg %1. The command resumes in the foreground and can interact with the terminal.