Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

jobs Command Guide for Listing Shell Jobs

jobs lists the jobs your current shell is managing — which ones are running, which are stopped, and which number to use with bg and fg.


What is jobs, and Why Use It?

The jobs command prints a table of the background and suspended jobs belonging to your current shell: each with a job number, a state (Running, Stopped, Done), and the command that started it. It is the control panel for the jobs you started with &, suspended with Ctrl-Z, or resumed with bg and fg — the shell’s own miniature process manager, scoped to one terminal session.

Why Learn jobs?

  • You need the job number: bg and fg take a job number (%1, %2); jobs is how you find out which is which.
  • Recover after suspending: Ctrl-Z freezes a job; jobs shows what is frozen so you can decide to continue it in the background.
  • Track long-running work: a script with several & jobs can be surveyed at a glance.
  • Shell-scoped truth: jobs shows only your shell’s jobs — the complement of ps, which shows the whole system.

jobs is small — one command, one screen — but it is the missing link between starting a job with &, pausing it with Ctrl-Z, and steering it with bg/fg.


1. Basic Usage: See What Is Running

# List all jobs of the current shell
jobs

# Start a job in the background, then list it
sleep 300 &
[1] 12345
jobs

Example output:

[1]+  Running                 sleep 300 &
[2]-  Stopped                 vim notes.txt
  • [1] and [2] are job numbers — the handles for bg/fg.
  • + marks the job that %%/+% refers to (the “current” job); - marks the previous one.

2. The State Column

StateMeaning
RunningThe job is executing.
StoppedSuspended — by Ctrl-Z or SIGSTOP.
DoneFinished; still listed until you reap it.
Exit nFinished with exit code n.
# Show only running jobs
jobs -r

# Show only stopped jobs
jobs -s
  • -r filters to running; -s filters to stopped. A stopped job is one fg or bg away from being alive again.

3. Job Numbers and Shortcuts

# The current job (the one with +)
fg %

# The previous job (the one with -)
fg %-

# Job number 1
fg %1

# By command name prefix
fg %sleep

# By string in the command line
fg %?vim
  • %% and %+ mean the current job; %- and %- mean the previous one.
  • %name matches a job whose command starts with name; %?str matches a substring.
  • These shortcuts work with bg, fg, kill, and jobs alike.

4. Keep the List Around

# Long listing: also show the command's PID
jobs -l

# Notify immediately when a job changes state
set -m        # job control on (usually default in interactive shells)
notify
  • jobs -l adds the process ID column — useful when you want to cross-reference with ps.
  • notify makes the shell announce state changes (“Done”) the moment they happen, instead of at the next prompt.

5. jobs vs. ps: Two Views of Processes

# All processes on the system
ps aux | grep sleep

# Only the jobs THIS shell started
jobs
  • jobs knows about shell jobs only — backgrounded commands, suspended commands.
  • ps sees everything: daemons, other users’ processes, and jobs from other terminals.
  • A job started in another terminal window is invisible to jobs; use ps there.

Common Pitfalls

  • jobs is per-shell: open a new terminal and the old jobs vanish from the list — they are still running, but this shell cannot steer them.
  • Job numbers are not PIDs: bg %1 takes the job number, not 12345. jobs -l shows both.
  • Done jobs linger: finished jobs stay in the list (marked Done) until the shell notices; they are harmless but can confuse.
  • Subshells hide jobs: ( sleep 100 & ) starts a job in a subshell — the parent shell’s jobs will not show it.
  • Ctrl-Z then closing the terminal leaves the job stopped; on logout the shell may SIGHUP it. Resume or kill suspended jobs before leaving.

PageRelationship
bgResumes a stopped job in the background — needs a jobs number.
fgBrings a job to the foreground — same job numbers.
psSystem-wide process view; jobs is the shell-local one.
killCan signal jobs too: kill %1.
nohupFor jobs that must survive the shell entirely.

jobs Cheatsheet

CommandWhat It Does
jobsList the shell’s jobs.
jobs -lList with PIDs.
jobs -rOnly running jobs.
jobs -sOnly stopped jobs.
jobs %%The current job.
fg %1Foreground job 1.
bg %2Background job 2.
kill %1Signal job 1.
fg %vimForeground a job whose command starts with vim.