# The `jobs` command The `jobs` command is used to display information about active jobs in the current shell session. Jobs are processes that have been started from the shell and can be managed using job control commands. ## Syntax ``` jobs [options] [job_spec] ``` ## Options Some popular option flags include: ``` -l List process IDs along with job information -p List only process IDs -n List only jobs that have changed status since last notification -r List only running jobs -s List only stopped jobs -x Replace job specifications with process IDs in command ``` ## Job States Jobs can be in different states: - **Running**: Job is currently executing - **Stopped**: Job is suspended (paused) - **Done**: Job has completed successfully - **Terminated**: Job was killed or ended abnormally ## Examples 1. List all current jobs ```bash jobs ``` 2. List jobs with process IDs ```bash jobs -l ``` 3. List only process IDs ```bash jobs -p ``` 4. List only running jobs ```bash jobs -r ``` 5. List only stopped jobs ```bash jobs -s ``` 6. Show status of specific job ```bash jobs %1 ``` ## Job Control Examples 1. Start a background job ```bash sleep 100 & ``` 2. Start multiple background jobs ```bash find / -name "*.log" > /tmp/logs.txt 2>/dev/null & ping google.com > /tmp/ping.txt & ``` 3. View all jobs ```bash jobs ``` Output might look like: ``` [1]- Running find / -name "*.log" > /tmp/logs.txt 2>/dev/null & [2]+ Running ping google.com > /tmp/ping.txt & ``` 4. Stop a running job (Ctrl+Z) ```bash # Start a command vim myfile.txt # Press Ctrl+Z to stop it # Then check jobs jobs ``` 5. Bring job to foreground ```bash fg %1 ``` 6. Send job to background ```bash bg %1 ``` 7. Kill a specific job ```bash kill %2 ``` ## 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 with Job Control 1. Start and manage multiple jobs ```bash # Start some background jobs sleep 300 & ping localhost > /dev/null & find /usr -name "*.conf" > /tmp/configs.txt 2>/dev/null & # List all jobs jobs -l # Bring first job to foreground fg %1 # Put it back to background (after stopping with Ctrl+Z) bg %1 # Kill second job kill %2 # Check remaining jobs jobs ``` 2. Working with stopped jobs ```bash # Start a text editor nano myfile.txt # Stop it with Ctrl+Z # Check jobs jobs # Resume in background bg # Resume in foreground fg ``` ## Use Cases - **Multitasking**: Running multiple commands simultaneously - **Long-running processes**: Managing tasks that take time to complete - **Background processing**: Running tasks while working on other things - **Job monitoring**: Keeping track of running processes - **Process management**: Controlling and organizing shell processes ## Related Commands - `fg` - Bring job to foreground - `bg` - Send job to background - `nohup` - Run command immune to hangups - `disown` - Remove job from job table - `kill` - Terminate job or process ## Important Notes - Jobs are specific to the current shell session - Job numbers are assigned sequentially - Jobs disappear when they complete or when you exit the shell - Use `&` at the end of a command to run it in background - Press `Ctrl+Z` to stop (suspend) a running job - Use `Ctrl+C` to terminate a running job ## Advanced Examples 1. Run command in background and disown it ```bash long_running_script.sh & disown %1 ``` 2. Check for completed jobs ```bash jobs -n ``` 3. Kill all jobs ```bash kill $(jobs -p) ``` The `jobs` command is essential for managing multiple processes and implementing effective workflow management in the shell. For more details, check the manual: `man jobs` or `help jobs`