jobs
Overview
The jobs
command displays active jobs in the current shell session. It shows background processes, suspended jobs, and their status, making it essential for job control in shell environments.
Syntax
jobs [options] [job_spec...]
Common Options
Option | Description |
---|---|
-l |
List process IDs |
-n |
Show only jobs that have changed status |
-p |
Show only process IDs |
-r |
Show only running jobs |
-s |
Show only stopped jobs |
-x command |
Replace job specs with PIDs |
Job States
State | Description |
---|---|
Running | Job is executing |
Stopped | Job is suspended |
Done | Job completed successfully |
Exit | Job exited with error |
Terminated | Job was killed |
Key Use Cases
- Monitor background jobs
- Job control management
- Process status checking
- Shell session management
- Script job tracking
Examples with Explanations
Example 1: List All Jobs
jobs
Shows all active jobs in current shell
Example 2: Show Process IDs
jobs -l
Lists jobs with their process IDs
Example 3: Running Jobs Only
jobs -r
Shows only currently running jobs
Example 4: Stopped Jobs Only
jobs -s
Shows only suspended/stopped jobs
Job Control Basics
Start background job:
command &
Suspend current job:
Ctrl+Z
Resume in background:
bg %1
Resume in foreground:
fg %1
Job Specification
Format | Description |
---|---|
%n |
Job number n |
%string |
Job whose command begins with string |
%?string |
Job whose command contains string |
%% or %+ |
Current job |
%- |
Previous job |
Common Usage Patterns
Check job status:
jobs -l | grep Running
Count active jobs:
jobs | wc -l
Find specific job:
jobs | grep command_name
Understanding Output
Typical output format:
[1]+ Running long_command &
[2]- Stopped vim file.txt
[3] Done make all
Components: - [1]: Job number - +: Current job - -: Previous job - Running/Stopped/Done: Job state - Command: The command being executed
Advanced Usage
Show only PIDs:
jobs -p
Changed status only:
jobs -n
Execute with job replacement:
jobs -x echo %1
Performance Analysis
- Instant operation
- No system resource usage
- Shell-specific information
- Real-time status
- Efficient for job management
Best Practices
- Regular job status checking
- Clean up completed jobs
- Use job control effectively
- Monitor long-running processes
- Understand job specifications
Job Management
Background a running job:
# Ctrl+Z to suspend bg %1 # Resume in background
Foreground a background job:
fg %1
Kill a job:
kill %1
Scripting Applications
Wait for jobs to complete:
#!/bin/bash command1 & command2 & command3 & while [ $(jobs -r | wc -l) -gt 0 ]; do sleep 1 done echo "All jobs completed"
Job monitoring:
monitor_jobs() { while true; do if jobs -r | grep -q .; then echo "$(date): $(jobs -r | wc -l) jobs running" fi sleep 10 done }
Integration Examples
With process management:
# Start multiple jobs for i in {1..5}; do long_task $i & done # Monitor progress jobs -l
Conditional job control:
if jobs -r | grep -q "backup"; then echo "Backup still running" else start_backup & fi
Job Cleanup
Remove completed jobs:
jobs -n # Shows status changes
Kill all jobs:
kill $(jobs -p)
Wait for all jobs:
wait # Built-in command
Shell-Specific Behavior
Different shells handle jobs differently: - Bash: Full job control support - Zsh: Enhanced job control - Fish: Modern job management - Dash: Limited job control
Troubleshooting
- Jobs not showing up
- Job control disabled
- Shell session issues
- Process orphaning
- Job state confusion
Security Considerations
- Monitor background processes
- Clean up abandoned jobs
- Check for unauthorized processes
- Resource usage monitoring
- Process privilege checking
Automation Examples
Parallel processing:
#!/bin/bash MAX_JOBS=4 process_file() { # Process file in background heavy_processing "$1" & # Limit concurrent jobs while [ $(jobs -r | wc -l) -ge $MAX_JOBS ]; do sleep 1 done } for file in *.txt; do process_file "$file" done # Wait for all to complete wait
Job status reporting:
report_jobs() { echo "Job Status Report - $(date)" echo "Running: $(jobs -r | wc -l)" echo "Stopped: $(jobs -s | wc -l)" echo "Total: $(jobs | wc -l)" }