Synopsis
Start a harmless waiting program in a terminal, locate it, and control its execution from the same shell. Use an interactive /bin/sh session under an ordinary account on OpenBSD or Debian. Start a fresh practice shell if other jobs are already running there.
Start work in the background
A process is a running instance of a program. The operating system assigns it a process ID (PID). A shell job is a command or pipeline the interactive shell tracks as a unit. One job can contain several processes.
Run a five-minute wait in the background and save its PID.
sleep 300 &
sleep_pid=$!The ampersand allows the shell to print another prompt immediately. The shell expands $! to the PID of its most recent background command. In this case, the job consists of a single sleep process.
Examine the shell’s job list and the operating system’s process listing.
jobs
ps -p "$sleep_pid"In jobs, find a short job number in brackets and the command sleep 300. In ps, find the PID in the column marked PID. The two numbers serve different purposes. The shell assigns a job number within the session. The PID identifies a process on the running system. OpenBSD sh(1) · OpenBSD ps(1) · Debian sh(1)
Suspend and resume the job
Bring the current job to the foreground. With only the exercise job in the list, no job number is necessary.
fgThe prompt disappears while the shell waits. Hold Ctrl and press Z. Under the normal terminal settings, the terminal sends a suspend signal to the foreground job and the shell regains control. Inspect the job.
jobsLook for a stopped or suspended state and the sleep command. A stopped process still exists. Resume it in the background.
bg
jobsThe listing should identify a running job. If five minutes have elapsed, sleep may already have finished. Start a new sleep 300 & and repeat the PID assignment before continuing.
End the foreground job
Bring the exercise job forward once more.
fgHold Ctrl and press C. This time the terminal sends an interrupt signal. The default action for sleep ends the process. Check the shell’s list.
jobsThe job should no longer be running. Some shells display a final status once before dropping the entry. A shell that reports unavailable job control may be reading from a script or a pipe. Open an interactive terminal and repeat the exercise there.
Continue with Turn a pipeline into a shell script.