Linux Comm Jobs
[ Linux Command Encyclopedia](#)
`jobs` is a built-in command in Linux/Unix systems used to view and manage background tasks in the current shell session. It allows users to:
* View the status of all background running jobs
* Control job switching between foreground and background
* Manage execution of multiple concurrent tasks
When you run time-consuming commands in the terminal, the `jobs` command can help you efficiently manage these background processes.
* * *
## Basic Syntax
jobs
### Common Options
| Option | Description |
| --- | --- |
| `-l` | Display job's PID (Process ID) |
| `-p` | Only display job's process group ID |
| `-n` | Only display jobs whose status has changed since last notification |
| `-r` | Only display running jobs |
| `-s` | Only display stopped jobs |
* * *
## Core Functions Explained
### 1. Viewing Background Jobs
The simplest usage is to directly enter the `jobs` command:
## Example
$ jobs
- running sleep 100&
+ stopped vim file.txt
Output explanation:
* ``: Job number (Job ID)
* `-`/`+`: `-` indicates the previous job, `+` indicates the current job
* `running`/`stopped`: Job status
* The last part is the command that started the job
### 2. Displaying Detailed Process Information
Use the `-l` option to view the job's process ID:
## Example
$ jobs -l
- 12345 running sleep 100&
+ 12356 stopped vim file.txt
### 3. Filtering Jobs by Specific Status
## Example
# View only running jobs
$ jobs -r
# View only stopped jobs
$ jobs -s
* * *
## Practical Application Scenarios
### Scenario 1: Running Long Tasks in Background
## Example
# Start a time-consuming task and put it in background
$ long_running_command &
# View background jobs
$ jobs
+ running long_running_command &
### Scenario 2: Pausing and Resuming Jobs
## Example
# Start vim
$ vim file.txt
# Press Ctrl+Z to pause vim
^Z
+ stopped vim file.txt
# View stopped jobs
$ jobs -s
+ stopped vim file.txt
# Resume vim in foreground
$ fg %1
### Scenario 3: Managing Multiple Background Jobs
## Example
# Start three background tasks
$ sleep 100&
$ sleep 200&
$ sleep 300&
# View all jobs
$ jobs -l
12345 running sleep 100&
- 12356 running sleep 200&
+ 12367 running sleep 300&
# Resume job 2 in foreground
$ fg %2
# Terminate job 3
$ kill %3
* * *
## Job Control Command Combinations
`jobs` is often used together with other job control commands:
| Command | Description | Example |
| --- | --- | --- |
| `&` | Run command in background | `command &` |
| `Ctrl+Z` | Pause current foreground job | Key combination |
| `fg` | Resume job in foreground | `fg %1` |
| `bg` | Continue stopped job in background | `bg %2` |
| `kill` | Terminate specified job | `kill %3` |
* * *
## Frequently Asked Questions
### Q1: Why do background jobs terminate after closing the terminal?
A: By default, background jobs are associated with the terminal session. Using `nohup` or `disown` can keep jobs running after the terminal closes:
## Example
$ nohup long_running_command &
### Q2: How to reference a specific job?
A: Use `%` followed by the job number:
* `%1`: Job 1
* `%2`: Job 2
* `%+` or `%%`: Current job
* `%-`: Previous job
### Q3: What's the difference between jobs and ps commands?
A:
* `jobs` only displays jobs in the current shell session
* `ps` displays all processes in the system, including processes from other users and terminals
* * *
## Best Practice Suggestions
1. **Redirect output for background jobs**: Avoid output interfering with current terminal
$ command > output.log 2>&1 &
2. **Use descriptive names**: Easier to identify different jobs
$ (sleep 300; echo "Done") &
3. **Regularly check job status**: Prevent accumulation of unfinished jobs
$ watch -n 60 jobs
4. **Use nohup for important jobs**: Ensure continued execution after terminal disconnects
$ nohup important_command &
* * *
## Summary Flowchart
!(#)
[ Linux Command Encyclopedia](#)
YouTip