bg
Resume a stopped job and run it in the background
By CMD Script Team · 5 min read · Last updated
bg [JOBSPEC]Options
| Flag | Description |
|---|---|
(no argument) | Resume the current job (the one marked with + in jobs output) in the background |
%n | Resume job number n in the background, e.g. bg %2 |
%+ | Resume the current job explicitly (same as no argument) |
%- | Resume the previous job (the one marked with - in jobs output) |
%string | Resume the job whose command starts with string, e.g. bg %rsync |
%?string | Resume the job whose command contains string anywhere |
Distribution compatibility
- Ubuntu
- Debian
- Fedora
- Arch
- macOS
What it does
bg is a shell built-in (available in bash, zsh, and other job-control-capable shells)
that resumes a stopped job and continues running it in the background instead of the
foreground. The typical workflow is: you start a long-running command, realize you need
your terminal back, press Ctrl+Z to suspend it (sending SIGTSTP), and then run bg to
let it keep running without blocking your prompt. It works together with fg (bring a
job to the foreground), jobs (list current jobs), and the & suffix (start a command
already in the background) as the core of shell job control.
Beginner examples
- Start a long command, then press
Ctrl+Zto suspend it bg— resume the most recently stopped job, now running in the backgroundjobs— list current jobs and their state (Running, Stopped) with job numbersbg %1— resume job number 1 specifically, in the background
# start something long-running
find / -name "*.log" > /tmp/all_logs.txt
# press Ctrl+Z to stop it, then:
bg
Advanced examples
- Background multiple stopped jobs by job number:
bg %1 %2 - Resume the previous (not current) job explicitly:
bg %- - Match a job by its command name instead of number:
bg %rsync - Check a backgrounded job's status afterward:
jobs -l - Redirect a job's output before backgrounding it (needs to be set up before stopping, or
reopened) so it doesn't clutter the terminal:
long_command > output.log 2>&1thenCtrl+Zthenbg
jobs -l
bg %2
Common mistakes
- Trying to background a job that never ran in the foreground of the current shell (e.g.
a process from another terminal) —
bgonly operates on jobs known to the current shell's job table, not arbitrary system processes; usekill/psfor those. - Forgetting that a backgrounded job still writes to the terminal's stdout/stderr unless redirected, which can interleave confusingly with whatever you type next.
- Assuming
bgrestarts a command from the beginning — it doesn't; it resumes exactly where the stopped process left off (via SIGCONT), preserving its existing state. - Closing the terminal (or losing the SSH session) after backgrounding a job and being
surprised it dies anyway — a backgrounded job is still a child of that shell and, in
most default configurations, gets SIGHUP when the shell exits, unless started with
nohup/disownor run insidetmux/screen.
Tips
- Run
jobsbeforebg/fgif more than one job is stopped, so you background the right one by its job number rather than assuming it's "the current job." - Use
disownafterbgif you want the job to survive even after you close the terminal session, since a plain backgrounded job is still tied to the shell. - Combine with output redirection at launch time (
command > out.log 2>&1 &) to avoid ever needing to background it manually — cleaner than stopping and resuming. - If a job needs input from the terminal, backgrounding it with
bgwill usually cause it to stop again (SIGTTIN) the moment it tries to read stdin — redirect its input from a file or/dev/nullinstead.
Best practices
- Prefer starting commands with a trailing
&from the outset when you already know you want them in the background, reservingCtrl+Z+bgfor cases where you change your mind mid-run. - Pair long background jobs with
nohupordisown(and output redirection) if they need to survive terminal/session closure, rather than relying on the shell staying open. - Use a terminal multiplexer (
tmux,screen) for genuinely long-running background work on a remote server instead of relying solely on shell job control, which is tied to that one shell session. - Check
jobsregularly during a session with several backgrounded tasks so you don't lose track of which job number corresponds to which command.
Try it yourself
A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.
Real-world use cases
- Freeing up a terminal to run other commands while a large
rsyncorfindoperation you started in the foreground keeps running in the background. - Backgrounding a command you started without redirecting output, so you can keep working
while occasionally checking on it with
jobsor bringing it back withfg. - Managing several ad hoc long-running tasks in one interactive SSH session (e.g. a build,
a log tail, a sync) by stopping and backgrounding each with
Ctrl+Z+bg, then switching between them withfg %n.
Common interview questions
- What's the difference between running a command with & and stopping it with Ctrl+Z
then running bg?
&backgrounds a command from the moment it starts;Ctrl+Z+bgis for a command already running in the foreground that you decide to move to the background afterward, without restarting it. - What happens to a backgrounded job if you close the terminal? By default it
typically receives SIGHUP and terminates, unless it was started with
nohup, detached withdisown, or run inside a persistent session liketmux/screen. - How do you bring a specific backgrounded job back to the foreground?
fg %n, wherenis the job number shown byjobs(orfg %-for the previous job,fgalone for the current one).
Frequently Asked Questions
What does bg actually do?
bg resumes a job that was previously stopped (typically by pressing Ctrl+Z) and lets it keep running, but in the background, so it continues execution without occupying your terminal's foreground and without you needing to wait for it to finish before typing more commands.
What's the difference between bg and just appending & to a command?
Appending & starts a command in the background from the moment you launch it. bg is for a command that's already running in the foreground (or was stopped with Ctrl+Z) and that you now want to move to the background without restarting it.
How do I know which job number to give bg?
Run jobs to list all jobs with their numbers, e.g. [1]+ Stopped rsync .... Then use bg %1 (or just bg if it's the current/only job, marked with +).
Can I bring a background job back to the foreground?
Yes, use fg (optionally with a jobspec like fg %1) to bring a backgrounded or stopped job back into the foreground, where it will occupy the terminal again.
Cheat sheet
Download a quick-reference cheat sheet for bg.