Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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-Z is a pause button; bg is 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 nohup or 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 with Ctrl-Z, steer with jobs/bg/fg.

bg is 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 &
  • bg without arguments resumes the current job (the one marked + in jobs).
  • bg %1 resumes 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, and jobs — 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.
  • bg earns its keep when you started something in the foreground, changed your mind, or accidentally hit Ctrl-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 bg job is still a child of your shell; when the shell exits it may send SIGHUP to its jobs.
  • For work that must outlive the session, use nohup, setsid, or a systemd unit.
  • bg also 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

  • bg on a running job does nothing — it only resumes stopped jobs. Check jobs first.
  • Bare bg uses the current job — if several are stopped, say which one: bg %2.
  • bg jobs die with the terminal unless protected; use nohup for 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-Z does not kill, it suspends — the job keeps its memory and state; bg continues exactly where it stopped.

PageRelationship
jobsLists the jobs bg acts on — find the number first.
fgThe opposite move: bring the job back to the foreground.
nohupFor background jobs that must survive logout.
waitPause a script until background jobs finish.
psSee the job’s process from the system-wide view.

bg Cheatsheet

CommandWhat It Does
bgResume the current stopped job.
bg %1Resume job number 1.
bg %+Resume the current job.
bg %-Resume the previous job.
bg %nameResume a job by command-name prefix.
jobs -sShow only stopped jobs (candidates for bg).
cmd &Start a job in the background from the outset.