Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux jobs command

Linux jobs command

Linux Command Reference Linux Command Reference

jobs is a built-in command in Linux/Unix systems used to view and manage background jobs in the current shell session. It allows you to:

When you run a long-running command in the terminal,jobs the command helps you manage these background processes efficiently.


Basic Syntax



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

Common options

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

Detailed core functions

1. View background jobs

The simplest usage is to simply type jobs Command:

Example

$

jobs

[

1

]

 - running    

sleep

100

&

[

2

]

 + stopped    

vim

file.txt

Output explanation:

2. Display detailed process information

Use -l Use the option to view the process ID of jobs:

Example

$

jobs

-l

[

1

]

 -

12345

running    

sleep

100

&

[

2

]

 +

12356

stopped    

vim

file.txt

3. Filter jobs by a specific status

Example

View only running jobs

$

jobs

-r

View only stopped jobs

$

jobs

-s


Practical Application Scenarios

Scenario 1: Run a long task in the background

Example

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: Pause and resume jobs

Example

Start vim

$

vim

file.txt

Press Ctrl+Z to pause vim

^Z

[

1

]

 + stopped    

vim

file.txt

View stopped jobs

$

jobs

-s

[

1

]

 + stopped    

vim

file.txt

Resume vim in the foreground

$

fg

%

1

Scenario 3: Manage multiple background jobs

Example

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

&

Resume job 2 in the foreground

$

fg

%

2

Terminate job 3

$

kill

%

3


Job control command combinations

jobs It is usually used together with other job control commands:

Command Description Example
& Put a command in the background command &
Ctrl+Z Pause the current foreground job Key combination
fg Resume a job in the foreground fg %1
bg Continue 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 the terminal is closed?

A: By default, background jobs are associated with the terminal session. Use nohup or disown to let jobs continue running after the terminal is closed:

Example

$

nohup

long_running_command

&

Q2: How do I refer to a specific job?

A: Use % Add the job number:

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

A:


Best Practice Recommendations

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

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

$ (sleep 300; echo "Done") & 3.

Check job status regularly: Avoid accumulating a large number of unfinished jobs

$ watch -n 60 jobs
4.

Use nohup for important jobs: Ensure they keep running after the terminal disconnects

$ nohup important_command &


Summary flowchart

Linux Command Reference Linux Command Reference