Job Control
100%

Processes · Lesson 11

Job Control

Learn how an interactive shell manages foreground, background, and stopped jobs.

Interactive shells use job control to coordinate pipelines within one terminal session. A job can contain one process or an entire pipeline, normally grouped into a process group so the terminal and shell can act on it as a unit.

Starting a Background Job

Append & to start a pipeline asynchronously:

$ sleep 1000 &
[1] 18420

The shell returns a prompt without waiting for the job to finish. Background status does not automatically redirect output, detach the controlling terminal, or make the job survive logout. Redirect input and output explicitly when required, and use a service manager, scheduler, or terminal multiplexer for work that must outlive the interactive shell.

A background job that attempts to read from the controlling terminal is normally stopped with SIGTTIN because it is not the terminal's foreground process group.

What does a trailing & ask an interactive shell to do?

Listing Shell Jobs

The jobs builtin lists jobs known to the current shell:

$ jobs
[1]    Running    sleep 1000 &
[2]-   Running    sleep 1001 &
[3]+   Stopped    sleep 1002

The bracketed number is a shell job ID, not a PID. A % prefix forms a job specification such as %1. The + marker identifies the current job selected by many commands when no operand is given; - identifies the previous job.

Because the job table belongs to one shell, another terminal's shell normally cannot list or address these jobs through its own jobs, fg, or bg builtins.

What does the jobs builtin list?

Stopping and Continuing a Job

While a job is in the foreground, pressing Ctrl-Z normally makes the terminal send SIGTSTP to its foreground process group. The shell regains control after the job stops:

$ sleep 1002
^Z
[3]+  Stopped    sleep 1002

Continue the current stopped job in the background with:

$ bg

bg sends a continuation signal and leaves the job outside the terminal foreground. It is useful only for a stopped job; a command already running in the background does not need to be resumed.

What does bg %3 do to stopped job 3?

Moving a Job to the Foreground

Use fg with a job specification to make a job the terminal's foreground process group and wait for it:

$ fg %1

Without an operand, fg normally selects the current job marked +. A stopped job is continued as it enters the foreground.

What does fg %1 do?

Signaling a Job

Shells let kill accept a job specification:

$ kill -TERM %1

This normally signals the job's process group rather than just one pipeline member. Inspect the selected job first and use SIGTERM before considering forceful escalation. Job specifications are shell syntax; scripts and external tools more commonly work with verified PIDs or process-group IDs.

Which operand refers to shell job 1 rather than process ID 1?

Lesson complete

You finished Job Control

You can now move jobs deliberately between shell-controlled states.

  • Use & to start a background job without automatic detachment.

  • Use jobs to inspect the current shell's job table.

  • Stop with Ctrl-Z and continue in the background with bg.

  • Return a selected job to the terminal with fg.

  • Address shell jobs with %JOB_ID when sending signals.

Keep your learning progress

Create a free account to save this lesson and continue learning on any device.

Create a free account
Back to Processes