Skip to content

bg Command Cheat Sheet

The bg (background) command resumes suspended jobs and keeps them running in the background. It is a fundamental part of the shell's job control system, allowing you to multitask within a single terminal session.


Synopsis

bg [job_spec ...]

Description

When a job is suspended (usually by pressing Ctrl+Z), it is stopped and control returns to the shell. The bg command restarts this stopped job but continues its execution in the background, freeing up your terminal for other commands.


Basic Usage

Resume Current Job

bg

Resumes the most recently suspended job in the background.

Resume Specific Job

bg %1

Resumes job number 1.

Batch Resume

bg %1 %2

Resumes jobs 1 and 2.


Job Control Workflow

1. Start a Long Process

sleep 1000

2. Suspend Process

Press Ctrl+Z.

Output:

[1]+  Stopped                 sleep 1000

3. Resume in Background

bg

Output:

[1]+ sleep 1000 &

4. Verify Status

jobs

Output:

[1]+  Running                 sleep 1000 &


Job Specifications (job_spec)

Arguments to bg (and fg, kill, wait, disown) are job specifications.

Specifier Meaning
%n Job number n (e.g., %1)
%s Job whose command line starts with string s
%?s Job whose command line contains string s
%% or %+ Current job (the most recently stopped or backgrounded)
%- Previous job

Examples

bg %1       # Job 1
bg %vim     # Job starting with "vim"
bg %?sleep  # Job containing "sleep"
bg %+       # Current job

jobs

List active jobs.

jobs -l

Shows job IDs and Process IDs (PIDs).

fg

Bring a background job to the foreground.

fg %1

Ctrl+Z

Suspend the current foreground job.

& (Ampersand)

Start a command in the background immediately.

sleep 1000 &

Advanced Job Control

Disowning Jobs

If you close the terminal, background jobs (started with & or moved with bg) usually terminate (receive SIGHUP). To prevent this:

disown %1

Or start with nohup:

nohup long_script.sh &

Backgrounding Suspended SSH

If you are using SSH and it hangs: 1. Press Enter, then ~ (tilde), then Ctrl+Z. 2. This suspends the SSH client. 3. Resume with bg (keeps connection open but backgrounded).

Handling Output

Background jobs still output to the terminal (stdout/stderr).

Best Practice: Redirect output when backgrounding.

command > log.txt 2>&1 &

If you forgot to redirect: 1. Suspend (Ctrl+Z) 2. Resume in background (bg) 3. Note: You can't easily redirect output of a running process without tools like gdb or reptyr.


Practical Examples

Multitasking in Vim

  1. Open a file: vim file.txt
  2. Need to run a command? Press Ctrl+Z.
  3. Run command: ls -la
  4. Return to Vim: fg

Running Multiple Servers

python3 -m http.server 8000 &
python3 -m http.server 8001 &
jobs
# [1] Running python3...
# [2] Running python3...

Resume Stopped Backup

  1. Start backup: tar -czf backup.tar.gz /data
  2. Realize it takes too long.
  3. Suspend: Ctrl+Z
  4. Background: bg
  5. Continue working.

Signals and Background Jobs

SIGTTOU / SIGTTIN

Background jobs stop if they try to read from (SIGTTIN) or write to (SIGTTOU) the terminal (if configured).

  • If a background job needs input, it will stop.
  • Solution: Bring to foreground (fg) provider input, or redirect input from file.
# Will stop immediately
cat > file.txt &

# [1]+  Stopped                 cat > file.txt

Shell Configuration

Shopt Options (Bash)

  • checkjobs: Warns about running jobs before exiting shell.
  • huponexit: Sends SIGHUP to all jobs when shell exits (default on).
shopt -s checkjobs

Troubleshooting

"bg: no current job"

You tried to run bg but there are no stopped jobs.

"bg: job not found: 5"

Job ID 5 does not exist. Check with jobs.

Process Still Writing to Screen

The background process is noisy. Solution: Unfortunately, you cannot easily silence a running background process from the shell. Workaround if you have disown: 1. disown %1 2. Close terminal (process continues).

Or use reptyr to attach and redirect.


Exit Status

Code Meaning
0 Success
1 No job found or other error

Comparison

Command Action
command & Start in background
Ctrl+Z Suspend (Stop)
bg Resume suspended job in background
fg Resume suspended/background job in foreground
jobs List jobs
kill %1 Terminate job 1

Tips

  1. Use & first - If you know it will be long, start with &.
  2. Redirect I/O - Always redirect stdin/stdout/stderr for background jobs.
  3. Use nohup or tmux - For truly persistent background tasks usable after logout.
  4. Job IDs vs PIDs - bg uses Job IDs (%1), kill can use both (%1 or 1234).
  5. Chain Commands - sleep 10; echo done &
  6. Logical Operators - make && make install &