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
Related Commands
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
- Open a file:
vim file.txt - Need to run a command? Press
Ctrl+Z. - Run command:
ls -la - 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
- Start backup:
tar -czf backup.tar.gz /data - Realize it takes too long.
- Suspend:
Ctrl+Z - Background:
bg - 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
- Use
&first - If you know it will be long, start with&. - Redirect I/O - Always redirect stdin/stdout/stderr for background jobs.
- Use
nohuportmux- For truly persistent background tasks usable after logout. - Job IDs vs PIDs -
bguses Job IDs (%1),killcan use both (%1or1234). - Chain Commands -
sleep 10; echo done& - Logical Operators -
make && make install &