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.
sleep 300 &
jobsThe 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.
sleep 200
# press Ctrl-Z, then:
jobs
bg %1
fg %1Pressing 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.
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.
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.
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.