Linux Command Reference ยท Processes and Signals
Bash jobs command
Reference for the Bash jobs builtin: list background and stopped jobs, read the job table, show process IDs, filter by state and refer to jobs with %1, %% and %+.
jobs is a Bash builtin that lists the jobs the current shell is managing: commands you started with &, or stopped with Ctrl+Z. A job is a pipeline the shell started and keeps track of; each gets a small number ([1], [2]) that you use with fg, bg, kill and wait.
Jobs belong to one shell. A second terminal, or a script that started its own children, has its own list, and jobs never shows processes started by other shells. When a job has finished, jobs reports it once as Done and then forgets it.
jobs [-lnprs] [JOBSPEC...]
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
jobs [-lnprs] [-x COMMAND [ARGS]] [JOBSPEC ...]
| Option | Description |
|---|---|
-l |
Long format: also show process IDs. |
-p |
Print only the process IDs (the process group leader of each job). |
-n |
Only jobs whose status changed since the last notification. |
-r |
Only running jobs. |
-s |
Only stopped jobs. |
-x CMD |
Replace each jobspec in CMD with its process ID, then run it. |
| Job specification | Meaning |
|---|---|
%N |
Job number N. |
%%, %+ |
The current job (the most recently started or stopped one, marked +). |
%- |
The previous job (marked -). |
%string |
The job whose command begins with string. |
%?string |
The job whose command contains string. |
Returns: 0, or non-zero if a given jobspec does not exist.
Usage notes
- Interactive shells only for the full feature set. In a script job control is off by default, so Ctrl+Z,
fgandbgdo not work unless you runset -m. Background jobs andjobsstill work. - The number is not the process ID.
[1]is the job number; the PID is shown byjobs -lor in$!right after starting a job. - Pipelines are one job.
sort big | uniq > out &is job[1]even though it has two processes.jobs -pprints the leader's PID. Doneis shown once. After a job finishes, the nextjobs(or the next prompt) reports it and removes it from the table.- Leaving the shell. Closing a terminal sends
SIGHUPto the jobs. Usenohupordisownto keep them alive. - A subshell has no jobs.
$(jobs)and( jobs )inside the shell list nothing from the parent in most cases; runjobsin the main shell. - Differences: in
dash(/bin/sh) the output format differs, anddisowndoes not exist.
Related
bgandfg,wait,nohup,kill- Lessons: Jobs and background