# The `bg` command The `bg` command is used to put stopped jobs in the background, allowing them to continue running while you use the terminal for other tasks. It's part of the job control features in Unix-like shells. ## Syntax ``` bg [job_spec] ``` If no job specification is provided, `bg` operates on the current job (most recent job). ## Job Specifications You can refer to jobs using different formats: - `%1` - Job number 1 - `%+` or `%%` - Current job (most recent) - `%-` - Previous job - `%string` - Job whose command line starts with string - `%?string` - Job whose command line contains string ## Examples 1. Put the current stopped job in background ```bash bg ``` 2. Put specific job in background ```bash bg %1 ``` 3. Put multiple jobs in background ```bash bg %1 %2 %3 ``` ## Complete Job Control Workflow Here's a typical workflow demonstrating `bg` usage: 1. Start a long-running command ```bash find / -name "*.log" > /tmp/findlogs.txt 2>/dev/null ``` 2. Stop the job with Ctrl+Z ``` ^Z [1]+ Stopped find / -name "*.log" > /tmp/findlogs.txt 2>/dev/null ``` 3. Check jobs ```bash jobs ``` Output: ``` [1]+ Stopped find / -name "*.log" > /tmp/findlogs.txt 2>/dev/null ``` 4. Put the stopped job in background ```bash bg %1 ``` Output: ``` [1]+ find / -name "*.log" > /tmp/findlogs.txt 2>/dev/null & ``` 5. Verify the job is running in background ```bash jobs ``` Output: ``` [1]+ Running find / -name "*.log" > /tmp/findlogs.txt 2>/dev/null & ``` ## Practical Examples 1. Working with a text editor ```bash # Start editing a file vim myfile.txt # Stop with Ctrl+Z # Put it in background bg # Now you can run other commands while vim runs in background ls -la # Bring vim back to foreground when needed fg %1 ``` 2. Managing multiple background tasks ```bash # Start several tasks and stop them ping google.com > /tmp/ping1.txt # Ctrl+Z sleep 300 # Ctrl+Z tar czf backup.tar.gz /home/user/documents # Ctrl+Z # Check all stopped jobs jobs # Put all in background bg %1 bg %2 bg %3 # Or put specific ones bg %ping # Job starting with "ping" ``` 3. Starting command directly in background vs using bg ```bash # Method 1: Start directly in background find /usr -name "*.conf" > /tmp/configs.txt & # Method 2: Start normally, stop, then background find /usr -name "*.conf" > /tmp/configs.txt # Ctrl+Z bg ``` ## Related Commands - `fg` - Bring job to foreground - `jobs` - List active jobs - `kill` - Terminate job - `nohup` - Run command immune to hangups - `disown` - Remove job from job table ## Use Cases - **Multitasking**: Run multiple tasks simultaneously - **Long processes**: Let time-consuming tasks run while working on other things - **Interactive programs**: Temporarily background editors or interactive tools - **Development**: Background compilation while coding - **System administration**: Background monitoring while performing other tasks ## Important Notes - Jobs put in background with `bg` are still attached to the terminal - If you close the terminal, background jobs may be terminated - Use `nohup` or `disown` for persistent background processes - Background jobs cannot read from stdin (keyboard input) - You can use `fg` to bring background jobs back to foreground - Background jobs continue to write to stdout/stderr unless redirected ## Error Handling If `bg` fails, common reasons include: - Job doesn't exist - Job is already running - Job cannot be put in background (some interactive programs) ## Tips - Always check job status with `jobs` before and after using `bg` - Redirect output for background jobs to avoid cluttering the terminal - Use job control responsibly to avoid system resource issues - Consider using terminal multiplexers like `screen` or `tmux` for persistent sessions The `bg` command is essential for effective multitasking and job management in the shell environment. For more details, check the manual: `help bg`