fg
Overview
The fg
command brings background or suspended jobs to the foreground. It’s essential for job control, allowing you to interact with processes that are running in the background or have been suspended.
Syntax
fg [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
- Bring background jobs to foreground
- Resume suspended processes
- Interactive job control
- Process management
- Terminal multitasking
Examples with Explanations
Example 1: Bring Current Job to Foreground
fg
Brings the most recent job to foreground
Example 2: Bring Specific Job
fg %1
Brings job number 1 to foreground
Example 3: Bring Job by Command Name
fg %vim
Brings the vim job to foreground
Common Workflow
Start background job:
long_command & [1] 12345
Bring to foreground:
fg %1 long_command
Or resume suspended job:
# Job was suspended with Ctrl+Z fg %1
Job Control Cycle
State | Command | Next State |
---|---|---|
Running (fg) | Ctrl+Z |
Suspended |
Suspended | fg |
Running (fg) |
Suspended | bg |
Running (bg) |
Running (bg) | fg |
Running (fg) |
Common Usage Patterns
Quick foreground switch:
# List jobs jobs # Bring job to foreground fg %2
Toggle between jobs:
fg %1 # Switch to job 1 ^Z # Suspend fg %2 # Switch to job 2
Resume by partial command:
fg %?backup # Resume job containing "backup"
Advanced Usage
Bring job and check status:
jobs -l # List with PIDs fg %1 # Bring to foreground
Conditional foreground:
if jobs | grep -q "editor"; then fg %editor fi
Performance Analysis
- Instant operation
- No resource overhead
- Shell built-in command
- Efficient process control
- Real-time job switching
Best Practices
- Check job status before using fg
- Use specific job identifiers
- Understand job states
- Monitor job completion
- Handle job control signals properly
Error Handling
Job not found:
fg %99 # Error if job doesn't exist
No current job:
fg # Error if no jobs available
Job already in foreground:
fg %1 # No effect if already foreground
Interactive Examples
Editor workflow:
vim file.txt # Start editor ^Z # Suspend (Ctrl+Z) ls -la # Do other work fg # Resume editor
Compilation workflow:
make & # Start build in background vim source.c # Edit while building ^Z # Suspend editor fg %make # Check build progress
Scripting Applications
Job management function:
resume_job() { local job_pattern="$1" if jobs | grep -q "$job_pattern"; then fg %"$job_pattern" else echo "Job not found: $job_pattern" fi }
Interactive job selector:
select_job() { echo "Available jobs:" jobs read -p "Which job to foreground? " job_num fg %"$job_num" }
Integration Examples
With job monitoring:
# Monitor and manage jobs while true; do jobs read -p "Foreground job (or 'q' to quit): " choice case $choice in q) break ;; *) fg %"$choice" ;; esac done
Development environment:
# Start development tools code . & # Editor in background npm run dev & # Dev server in background # Work with tools interactively fg %code # Switch to editor ^Z # Suspend fg %npm # Check dev server
Signal Handling
When bringing job to foreground: - Job receives SIGCONT if suspended - Terminal control is transferred - Job can receive keyboard signals - Ctrl+C sends SIGINT to foreground job - Ctrl+Z sends SIGTSTP to suspend job
Shell Compatibility
Shell | Support | Features |
---|---|---|
Bash | Full | Complete job control |
Zsh | Enhanced | Advanced job management |
Fish | Modern | User-friendly syntax |
Dash | Basic | Limited job control |
Troubleshooting
Job control disabled:
set +m # Disable job control set -m # Enable job control
No controlling terminal
Job has exited
Permission issues
Shell doesn’t support job control
Security Considerations
- Verify job ownership
- Check process legitimacy
- Monitor resource usage
- Validate job commands
- Control process privileges
Alternative Methods
Direct process control:
kill -CONT $PID # Resume process
Screen/tmux sessions:
screen -r session_name tmux attach -t session_name
Process substitution:
command < <(background_process)
Real-world Scenarios
System administration:
# Start system monitor htop & # Do other work ps aux | grep problem_process # Return to monitor fg %htop
Development debugging:
# Start debugger gdb program & # Edit source vim source.c ^Z # Return to debugger fg %gdb
Job State Transitions
[Start] → Running(fg) → [Ctrl+Z] → Suspended
↓ ↑ ↓
& fg bg
↓ ↑ ↓
Running(bg) ←─────────────────── Running(bg)
Monitoring and Control
Job status checking:
jobs -l | grep Stopped fg %1 # Resume stopped job
Resource monitoring:
# Before bringing to foreground ps -p $(jobs -p %1) -o pid,pcpu,pmem,cmd fg %1