bg
Overview
The bg
command resumes suspended jobs in the background. It’s part of shell job control, allowing you to continue stopped processes without bringing them to the foreground.
Syntax
bg [job_spec...]
Job Specification
Format | Description |
---|---|
%n |
Job number n |
%string |
Job whose command begins with string |
%?string |
Job whose command contains string |
%% or %+ |
Current job (default) |
%- |
Previous job |
Key Use Cases
- Resume suspended processes
- Multitasking in terminal
- Job control management
- Process workflow optimization
- Shell session efficiency
Examples with Explanations
Example 1: Resume Current Job
bg
Resumes the most recently suspended job in background
Example 2: Resume Specific Job
bg %1
Resumes job number 1 in background
Example 3: Resume Multiple Jobs
bg %1 %2 %3
Resumes multiple jobs in background
Common Workflow
Start a command:
long_running_command
Suspend it (Ctrl+Z):
^Z [1]+ Stopped long_running_command
Resume in background:
bg %1 [1]+ long_running_command &
Job Control Sequence
Action | Command | Result |
---|---|---|
Start job | command |
Runs in foreground |
Suspend | Ctrl+Z |
Job stopped |
Background | bg |
Job runs in background |
Foreground | fg |
Job returns to foreground |
List jobs | jobs |
Show all jobs |
Common Usage Patterns
Quick background resume:
# Suspend current job ^Z # Resume in background bg
Manage multiple jobs:
jobs # List jobs bg %2 # Resume job 2
Resume by command name:
bg %vim # Resume vim job
Advanced Usage
Resume all stopped jobs:
for job in $(jobs -s | awk '{print $1}' | tr -d '[]+-'); do bg %$job done
Conditional resume:
if jobs -s | grep -q "backup"; then bg %backup fi
Performance Analysis
- Instant operation
- No resource overhead
- Shell built-in command
- Efficient job management
- Real-time process control
Best Practices
- Check job status before using bg
- Use specific job numbers for clarity
- Monitor background jobs regularly
- Combine with job listing commands
- Understand job control implications
Error Handling
Job not found:
bg %99 # Error if job doesn't exist
Job already running:
bg %1 # No effect if already running
No current job:
bg # Error if no current job
Scripting Applications
Automated job management:
#!/bin/bash # Start job long_process & JOB_PID=$! # Later, if needed to suspend and resume kill -STOP $JOB_PID kill -CONT $JOB_PID
Interactive job control:
manage_jobs() { echo "Stopped jobs:" jobs -s read -p "Resume which job? " job_num bg %$job_num }
Integration Examples
With job monitoring:
# Check and resume stopped jobs if jobs -s | grep -q .; then echo "Resuming stopped jobs..." jobs -s | while read line; do job_num=$(echo "$line" | awk '{print $1}' | tr -d '[]+-') bg %$job_num done fi
Workflow automation:
# Start multiple tasks task1 & task2 & task3 & # If any get suspended, resume them for job in $(jobs -s | awk '{print $1}' | tr -d '[]+-'); do bg %$job done
Shell Compatibility
Different shells support bg: - Bash: Full support - Zsh: Enhanced features - Fish: Modern syntax - Dash: Basic support - Tcsh: C-shell style
Troubleshooting
- Job control not enabled
- No jobs to resume
- Job already running
- Shell doesn’t support job control
- Process has exited
Security Considerations
- Monitor background processes
- Check process ownership
- Verify job legitimacy
- Resource usage monitoring
- Process privilege levels
Alternative Methods
Start directly in background:
command &
Use nohup for persistence:
nohup command &
Use screen/tmux for session management:
screen -S session_name command # Ctrl+A, D to detach
Real-world Examples
Development workflow:
# Start editor vim file.txt # Suspend to test ^Z # Resume editor in background bg # Run tests in foreground make test
System administration:
# Start backup backup_script # Suspend if needed ^Z # Resume in background bg # Continue other tasks
Monitoring Background Jobs
Regular status check:
watch -n 5 jobs
Job completion notification:
(sleep 100; echo "Job completed") &
Resource monitoring:
jobs -l | while read job; do pid=$(echo "$job" | awk '{print $2}') ps -p $pid -o pid,pcpu,pmem,cmd done