bg Command Guide for Resuming Jobs in the Background
bg takes a stopped job and resumes it in the background, so your terminal stays free while the job runs.
What is bg, and Why Use It?
The bg command resumes a stopped job in the background. When you press Ctrl-Z, the shell suspends the running job; bg <job> wakes it up again and lets it keep running while you continue typing. It is one of the three job-control commands — with jobs (what is running?) and fg (bring it back) — that turn a single terminal into a small multitasking workstation.
Why Learn bg?
- Un-freeze work you paused:
Ctrl-Zis a pause button;bgis the resume button that keeps the pause from blocking your shell. - Keep the terminal usable: a long computation can run while you keep working — no
nohupor detachment needed for short tasks. - One-word fix for “Oh no, it’s stopped”: half of all job-control confusion ends with
bg %1. - Completes the triangle: start with
&, pause withCtrl-Z, steer withjobs/bg/fg.
bgis the smallest command in this section and the one that makes the others click: it is what you type when a job is stopped and you want it still running but out of your face.
1. The Workflow: Stop, Then Resume
# Start a long job
sleep 300
# Press Ctrl-Z — the job is now Stopped
# List jobs to find the number
jobs
[1]+ Stopped sleep 300
# Resume it in the background
bg %1
[1]+ sleep 300 &
bgwithout arguments resumes the current job (the one marked+injobs).bg %1resumes job number 1 explicitly.
2. Job Shortcuts
bg %+ # the current job (same as bare bg)
bg %- # the previous job
bg %1 # job number 1
bg %sleep # a job whose command starts with "sleep"
bg %?vim # a job whose command contains "vim"
- The same shortcuts work in
fg,kill, andjobs— learn them once.
3. bg vs. Starting with & in the First Place
# Start directly in the background
sleep 300 &
# Or start in the foreground, stop it, then background it
sleep 300
# Ctrl-Z
bg
cmd &is simpler when you already know you want it backgrounded.bgearns its keep when you started something in the foreground, changed your mind, or accidentally hitCtrl-Z.
4. What bg Does NOT Do
# bg does NOT detach from the terminal — closing the terminal
# may still kill the job. For that:
nohup sleep 300 &
- A
bgjob is still a child of your shell; when the shell exits it may sendSIGHUPto its jobs. - For work that must outlive the session, use
nohup,setsid, or a systemd unit. bgalso cannot restart a job that has already finished — only stopped ones.
5. Checking the Result
jobs
[1]+ Running sleep 300 &
- After
bg, the job’s state changes from Stopped to Running. - When it finishes, the shell prints a “Done” message at the next prompt (or immediately with
notify).
Common Pitfalls
bgon a running job does nothing — it only resumes stopped jobs. Checkjobsfirst.- Bare
bguses the current job — if several are stopped, say which one:bg %2. bgjobs die with the terminal unless protected; usenohupfor anything important.- Output still prints to your terminal — a backgrounded job that writes to stdout will interleave with your prompts. Redirect it:
cmd > out.log 2>&1 &. Ctrl-Zdoes not kill, it suspends — the job keeps its memory and state;bgcontinues exactly where it stopped.
Related Pages
| Page | Relationship |
|---|---|
jobs | Lists the jobs bg acts on — find the number first. |
fg | The opposite move: bring the job back to the foreground. |
nohup | For background jobs that must survive logout. |
wait | Pause a script until background jobs finish. |
ps | See the job’s process from the system-wide view. |
bg Cheatsheet
| Command | What It Does |
|---|---|
bg | Resume the current stopped job. |
bg %1 | Resume job number 1. |
bg %+ | Resume the current job. |
bg %- | Resume the previous job. |
bg %name | Resume a job by command-name prefix. |
jobs -s | Show only stopped jobs (candidates for bg). |
cmd & | Start a job in the background from the outset. |