jobs

List background jobs.

Source: src/execution/builtins.f90:778-791, src/execution/jobs.f90

Synopsis

jobs [-lnprs] [jobspec ...]

Description

The jobs builtin displays the status of jobs started in the current shell session.

Options

OptionDescription
-lInclude process IDs
-nOnly jobs with changed status
-pProcess IDs only
-rRunning jobs only
-sStopped jobs only

Usage

List All Jobs

sleep 100 &
sleep 200 &
vim &
# Press Ctrl+Z in vim

jobs
# [1]   Running                 sleep 100 &
# [2]-  Running                 sleep 200 &
# [3]+  Stopped                 vim

With Process IDs

jobs -l
# [1]   12345 Running          sleep 100 &
# [2]-  12346 Running          sleep 200 &
# [3]+  12347 Stopped          vim

PIDs Only

jobs -p
# 12345
# 12346
# 12347

Running Jobs Only

jobs -r
# [1]   Running                 sleep 100 &
# [2]-  Running                 sleep 200 &

Stopped Jobs Only

jobs -s
# [3]+  Stopped                 vim

Job Indicators

SymbolMeaning
+Current job (default for fg/bg)
-Previous job
(none)Other jobs

Job States

StateDescription
RunningExecuting in background
StoppedSuspended (Ctrl+Z or SIGSTOP)
DoneCompleted (shown once)

Examples

Check for Running Jobs

if jobs -r | grep -q .; then
    echo "Background jobs running"
fi

Count Jobs

num_jobs=$(jobs | wc -l)
echo "$num_jobs jobs"

Kill All Jobs

kill $(jobs -p)

Wait for All Jobs

while jobs | grep -q Running; do
    sleep 1
done
echo "All jobs done"

Specific Job

jobs %1
# [1]+  Running                 sleep 100 &

jobs %vim
# Shows job with "vim" in command

Job Specification

SpecMeaning
%nJob number n
%%Current job
%+Current job
%-Previous job
%stringJob starting with string
%?stringJob containing string

Implementation

Source: jobs.f90

Jobs are stored in shell%jobs(MAX_JOBS) array with:

  • Job ID
  • Process group ID
  • Command line
  • State (RUNNING, STOPPED, DONE)
  • Notification flag

Exit Status

StatusCondition
0Success
1Invalid job specification

Notes

  • Jobs are shell-specific (not system-wide)
  • Done jobs are reported once then removed
  • Maximum jobs limited by MAX_JOBS
  • Job numbers are reused after completion

Related Commands

CommandPurpose
fgBring to foreground
bgContinue in background
killSend signal
waitWait for completion

See Also