Linux jobs Command

Linux 命令大全Linux Command Reference

jobsis 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 jobs
  • Control switching jobs between foreground and background
  • Manage the execution of multiple concurrent tasks

When you run long-running commands in the terminal,jobsthe command can help you efficiently manage these background processes.


Basic Syntax

jobs [选项] [作业标识符]

Common Options

Option Description
-l Display the job's PID (Process ID)
-p Display only the job's process group ID
-n Display only jobs whose status has changed since the last notification
-r Display only running jobs
-s Display only stopped jobs

Detailed Core Features

1. View Background Jobs

The simplest usage is to directly enter thejobscommand:

Examples

$ jobs
[1]  - running    sleep 100 &
[2]  + stopped    vim file.txt

Output Explanation:

  • [1]: 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. Display Detailed Process Information

Use the-loption to view the job's process ID:

Examples

$ jobs -l
[1]  - 12345 running    sleep 100 &
[2]  + 12356 stopped    vim file.txt

3. Filter Jobs by Specific Status

Examples

# View only running jobs
$ jobs -r

# View only stopped jobs
$ jobs -s

Practical Application Scenarios

Scenario 1: Running Long Tasks in the Background

Examples

# Start a time-consuming task and put it in the background
$ long_running_command &

# View background jobs
$ jobs
[1]  + running    long_running_command &

Scenario 2: Suspending and Resuming Jobs

Examples

# Start vim
$ vim file.txt

# Press Ctrl+Z to suspend vim
^Z
[1]  + stopped    vim file.txt

# View stopped jobs
$ jobs -s
[1]  + stopped    vim file.txt

# Restore vim to the foreground
$ fg %1

Scenario 3: Managing Multiple Background Jobs

Examples

# Start three background tasks
$ sleep 100 &
$ sleep 200 &
$ sleep 300 &

# View all jobs
$ jobs -l
[1]   12345 running    sleep 100 &
[2]  -12356 running    sleep 200 &
[3]  +12367 running    sleep 300 &

# Restore job 2 to the foreground
$ fg %2

# Terminate job 3
$ kill %3

Job Control Command Combinations

jobsUsually used together with other job control commands:

Command Description Example
& Put a command in the background command &
Ctrl+Z Suspend the current foreground job Key combination
fg Restore a job to the foreground fg %1
bg Continue running a stopped job in the background bg %2
kill Terminate a 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. Usingnohupordisowncan keep jobs running after the terminal is closed:

Examples

$ nohup long_running_command &

Q2: How to reference a specific job?

A: Use%plus the job number:

  • %1: Job 1
  • %2: Job 2
  • %+or%%: Current job
  • %-: Previous job

Q3: What is the difference between the jobs and ps commands?

A:

  • jobsOnly displays jobs in the current shell session
  • psDisplays all system processes, including processes from other users and terminals

Best Practice Recommendations

  1. Redirect output for background jobs: Avoid output interfering with the current terminal

    $ command > output.log 2>&1 &
  2. Use descriptive names: Makes it easier to identify different jobs

    $ (sleep 300; echo "Done") & 
  3. Regularly check job status: Prevent accumulation of a large number of unfinished jobs

    $ watch -n 60 jobs
  4. Use nohup for important jobs: Ensure they continue running after the terminal disconnects

    $ nohup important_command &

Summary Flowchart

Linux 命令大全Linux Command Reference

Other Extensions