A stopped terminal job does not need to block your shell forever. The bg command in Linux resumes a suspended job in the background, which is useful when a long-running command was paused with Ctrl+Z and you want the terminal prompt back without abandoning the job.
The examples here use Bash job-control output because Bash is the default interactive shell on many Linux distributions. Other job-control shells use the same idea but may format job messages differently.
Understand the bg Command
The bg command is a shell builtin, not a separate package or standalone binary. It works with the current shell’s job table, so it can resume jobs started from the same terminal session but cannot manage an unrelated process by PID. The GNU Bash manual documents bg with the other job-control builtins.
type bg
bg is a shell builtin
The basic syntax accepts one or more job specifications. If you omit the job specification, Bash uses the current job, usually the most recently stopped or foregrounded job.
bg [jobspec ...]
A job specification is not the same as a process ID. Shell job specs commonly use percent-prefixed forms such as %1, %2, %+ for the current job, and %- for the previous job. If your shell shows a different job number, substitute that number in the examples.
| Task | Command | What It Does |
|---|---|---|
| List shell jobs | jobs | Shows jobs tracked by the current shell. |
| Resume the current stopped job | bg | Continues the current stopped job in the background. |
| Resume a specific job | bg %2 | Continues job 2 in the background. |
| Return a job to the foreground | fg %1 | Attaches job 1 back to the terminal. |
| Wait for a job to finish | wait %1 | Waits for job 1 and returns its exit status. |
| Terminate a job | kill %1 | Sends the default termination signal to job 1. |
Resume a Stopped Job with bg
A common bg workflow starts with a foreground command that is taking longer than expected. The harmless sleep command makes a safe practice target because it does not change files or system state.
sleep 300
Press Ctrl+Z while the command is running. Bash suspends the foreground job and returns the prompt.
^Z [1]+ Stopped sleep 300
Resume the stopped job in the background:
bg
[1]+ sleep 300 &
Check the job table to confirm the job is now running in the background:
jobs
[1]+ Running sleep 300 &
End the practice job when you no longer need it:
kill %1
Resume a Specific Stopped Job
When several jobs are stopped, use jobs before running bg. The number in brackets is the shell job number, and the + marker identifies the current job.
jobs
[1]- Stopped sleep 300 [2]+ Stopped sleep 600
Resume job 1 while leaving job 2 stopped:
bg %1
[1]- sleep 300 &
Verify the mixed state when you are managing several jobs at once:
jobs
[1]- Running sleep 300 & [2]+ Stopped sleep 600
Clean up the practice jobs after checking the result:
kill %1 %2
Start New Commands in the Background
Use bg only after a job already exists and is stopped. To start a new command directly in the background, put an ampersand at the end of the command.
sleep 300 &
Bash prints a bracketed shell job number followed by the process ID it assigned to the command. Use the job number with bg, fg, jobs, and wait when you are working inside the same shell.
Stop the practice job before moving on:
kill %1
Control Output and Terminal Input
A background job still uses the same terminal unless you redirect it. For commands that print regular output, redirect standard output and standard error before starting the job. This short Bash loop writes timestamps to a demo log while the prompt stays available.
for _ in {1..5}; do
date
sleep 2
done >bg-output.log 2>&1 &
Remove the demo log after the background job finishes:
rm -f bg-output.log
If the command is already stopped, resume it with bg only when it can run without interactive input. A background job that tries to read from the terminal can stop again with a status such as Stopped (tty input) in jobs -l output.
jobs -l
Bring that job back to the foreground if it needs keyboard input:
fg %1
Wait, Foreground, or Detach Jobs
Backgrounding a job frees the prompt, but it does not make the job independent from the shell. Use fg to reattach it, wait to collect its exit status, disown -h to mark it so Bash does not send SIGHUP on shell exit, or disown to remove it from the active job table.
fg %1
wait %1
disown -h %1
disown %1
For scripts that start multiple background commands, wait for Bash background jobs explicitly instead of relying on interactive job-control commands. For long-running jobs that must survive logout, start them with a service manager, terminal multiplexer, nohup, or another persistence tool designed for that purpose.
Troubleshoot bg Command Errors
bash: bg: current: no such job
This error means the shell has no current job to resume. Check stopped jobs first, then pass the correct job specification to bg.
jobs -s
bg %1
bash: bg: %99: no such job
This error means the job specification does not exist in the current shell’s job table. Run jobs again because completed, killed, or shell-external processes are not valid bg targets.
jobs
bash: bg: no job control
This error usually appears in a non-interactive shell, such as a script or a remote one-shot command. Start the command with &, save $! if you need the process ID, and use wait for completion instead of bg.
sleep 5 &
pid=$!
wait "$pid"
The Background Job Keeps Printing to the Terminal
The job still owns the same terminal output streams. Stop it with Ctrl+Z only if safe, then decide whether to bring it back with fg, terminate it with kill %1, or restart it with output redirection.
kill %1
for _ in {1..5}; do
date
sleep 2
done >bg-output.log 2>&1 &
Remove the demo log after you no longer need it:
rm -f bg-output.log
Conclusion
The bg command is most useful when you understand the shell job table behind it: stop a foreground command, resume the right job specification, verify it with jobs, and redirect output before starting noisy background work. For scripted parallel work, pair background commands with wait instead of relying on interactive job control.


Formatting tips for your comment
You can use basic HTML to format your comment. Useful tags currently allowed in published comments:
<code>command</code>command<strong>bold</strong><em>italic</em><blockquote>quote</blockquote>