$_ CLI In Depth
Learning progress 0%
lesson processes signals job-control

Foreground, Background & Job Control

5 min read Module 10: Processes, Signals & Job Control Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:11

Process groups and the terminal

Every process belongs to a process group, and related processes are grouped together. A pipeline such as ls | sort is one group, and a shell job with several processes is one group. Each terminal has one foreground process group. Only that group receives the keyboard input and the terminal signals such as SIGINT and SIGTSTP. Any other group that tries to read from the terminal is stopped by the kernel with SIGTTIN, and one that writes with the right setting is stopped with SIGTTOU.

The shell uses process groups to implement job control. It puts each job in its own group, gives that group the terminal when the job is in the foreground, and takes the terminal back when the job is done or stopped.

Starting a job in the background

An ampersand at the end of a command starts it in the background, and the shell returns the prompt at once. The shell prints a job number in brackets and the PID of the job. The jobs builtin lists the jobs of the current shell, with their state and their command line.

BASH2 lines
sleep 300 &
jobs

The output shows the job as Running, with a number such as [1]+. The plus sign marks the current job, which is the default target of fg and bg. A job that is still running keeps writing to the terminal unless you redirect its output, which is why long background jobs usually redirect to a file.

Moving jobs with fg and bg

The fg builtin brings a job to the foreground and gives it the terminal. The bg builtin resumes a stopped job in the background. Both take a job spec such as %1, which means job number 1, and %% or %+ means the current job. %- means the previous job, and %sleep matches the job whose command starts with that word.

Ctrl-Z is the usual way to stop a foreground job. The shell reports that the job is stopped, and the job keeps its place in the job table. You can then resume it in the foreground with fg or in the background with bg.

BASH5 lines
sleep 200
# press Ctrl-Z, then:
jobs
bg %1
fg %1

Pressing Ctrl-Z stops the sleep, which then shows as Stopped in jobs. The bg command resumes it in the background, and fg brings it back. The comment line is there to remind you where the key press belongs, because a script cannot press a key.

Note

Job control is a feature of interactive shells. A non-interactive bash, such as one that runs a script, does not print job messages unless you turn on monitor mode with set -m. The job commands still exist, but the scripts you run in the lab work without them.

wait for background jobs

The wait builtin is the script-friendly way to join background work. With no arguments it waits for every background child of the current shell. With a PID or a job spec it waits for that one job and returns its status. This is the same wait that the previous unit described at the system call level.

BASH4 lines
sleep 1 &
sleep 2 &
wait
echo "all done"

The final message appears only after both sleeps finish, because wait with no arguments blocks until all background children have exited. The total time is about two seconds, not three, because the two sleeps overlap.

Jobs and the terminal close

A job runs under the shell that started it. When that shell exits, the job may be left running with the shell as its parent, or it may receive a SIGHUP, depending on how the shell was set up. The next unit covers the tools for keeping a job alive after you close the terminal, including nohup and disown.

Job control also changes how you should think about output. A job in the background that writes to the terminal interrupts whatever you are typing, so long jobs should write to a file. A job that tries to read from the terminal while it is in the background is stopped by the kernel, which is why a background command that expects input appears to hang. Redirect its input from a file or from /dev/null, and redirect its output to a log, and the job can run without holding the terminal at all.

The job table is local to one shell. A job started in one terminal is invisible to the shell in another terminal, even when both shells run as the same user. If you close the shell that owns a job, the job is no longer in any job table, and only ps can find it. Remember the PID of anything you start in the background, and write it to a file if you expect to come back to it later.

Hands-on: start, stop, and collect a job

In the lab, start a background sleep, record its PID, and confirm with ps that it is running. Then wait for it to finish, which here means sending it SIGTERM and collecting its status.

BASH4 lines
sleep 300 & echo $! > job.pid
ps -o pid,stat,args -p "$(cat job.pid)"
kill "$(cat job.pid)"
wait "$(cat job.pid)"; echo "status $?"

The ps line shows the state S and the command sleep 300. After the kill, the status is 143. The job went through the same states that the foreground jobs go through, but it started in the background and was never stopped by a key.

Signals, job control and hangups

Visualizer

A signal is a small numbered message the kernel delivers to a process. Some signals can be caught and some cannot. Job control and SIGHUP decide which signals a job gets.

1 / 5
Linux signal numbers (the same on this aarch64 machine)
SignalNo.Default actionCan a program catch it?
SIGHUP1terminateyes
SIGINT2terminateyes
SIGKILL9terminateno
SIGTERM15terminateyes
SIGCONT18continueyes
SIGSTOP19stopno
SIGTSTP20stopyes

The default action is what the kernel does when a program installs no handler. SIGKILL and SIGSTOP can never be caught or ignored.

sleep 30 & p=$!; kill -TERM $p; wait $p; echo "status $?"
Terminal lab · 2 steps · real Linux in your browser

Start background jobs and join them

Start a background job and record its PID, then wait for two jobs at once.

01
Start a sleep in the background
Add an ampersand to start sleep 300 in the background, then save its PID in job.pid. The process keeps running while you type other commands. ``bash sleep 300 & echo $! > job.pid `` You can confirm it with ps -o args= -p "$(cat job.pid)", which prints the sleep command.
Show hint for step 1
Use an ampersand after sleep 300, then echo $! into job.pid.
02
Wait for two background jobs
Start two one-second sleeps, keep each PID in its own variable, and call wait with both PIDs. When wait returns, write joined followed by the status of wait into joined.txt. ``bash sleep 1 & p1=$! sleep 1 & p2=$! wait $p1 $p2 echo "joined $?" > joined.txt `` The file should contain: joined 0
Show hint for step 2
Store each PID with $! right after starting its job. Pass both variables to a single wait.
Check yourself Question 1 of 4
Concept Check Listing jobs recall

Which builtin lists the jobs of the current shell with their states?

Recap & Key Takeaways

Each terminal has one foreground process group, which receives keyboard input and terminal signals such as SIGINT and SIGTSTP.
An ampersand starts a job in the background, jobs lists them, and fg and bg move a job between the foreground and the background.
Ctrl-Z stops the foreground job, and fg or bg resumes it, so a stopped job keeps its place in the job table.
wait joins background jobs in a script, and job control messages appear only in interactive shells or after set -m.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue