T6 Linux and Bash · Lesson 6
Processes
Everything that runs is a process with a number: running programs in the background and waiting for them, keeping track of jobs, stopping them with signals that ask and signals that tell, pausing and resuming at the keyboard, and finding what is running with ps and pgrep.
- say what a process and a PID are, and which commands start one
- run commands in the background with &, and wait for them
- keep track of jobs with jobs, %1 and their exit statuses
- stop processes with kill, and choose between TERM, INT and KILL
- pause, resume and move jobs with Ctrl+Z, bg and fg
- find running processes with ps, pgrep and pkill
The back cover
Everything that runs on a computer, from your shell to a web server, is a process with a number. Knowing how to find, pause and end them fixes a slow or stuck machine.
These pages show what a process is, and how to run programs in the background and wait for them. They show how to keep track of jobs, and how signals ask a process to end or tell it to. They show the keys that pause and resume a job, and the tools that find any process on the machine.
Before you start
The story: The engine that would not stop
Someone started the great drain engine last night and went home. Nobody knows who, or from which station, and the tunnel it serves was emptied hours ago. It is still running, and it is burning through the Underway's coal, and every other engine has slowed down to share what is left.
The stoker can hear it, but cannot tell which of forty engines it is.
Warden Rao opens the engine book. "Everything that runs down here has a number," she says. "Find the number. Ask it to stop. If it will not, then tell it."
Until now, each command ran, finished, and gave you the prompt back. Real work is often longer: a server that runs for months, a copy that takes an hour, a test run you want to leave going while you work. This lesson is about programs while they run: how to see them, start them in the background, and stop them.
It builds on these ideas. Each one has a tiny example, so you can check that you remember it.
| What you already know | A tiny example | Where it comes back |
|---|---|---|
| Ctrl+C stops a command. | sleep 100, then Ctrl+C | Now you learn how it works. |
| Status 130. | after Ctrl+C | One of a family of numbers. |
| The exit status. | echo $? | Every process leaves one. |
| Builtins and programs. | type cd | Only programs become processes. |
| The kernel. | it shares the hardware | It runs every process. |
| /proc. | live facts as files | One folder for each process. |
| Pipes. | grep FAIL log | wc -l | Each stage is a process. |
| Joining with &&. | sleep 2 && echo done | Joined steps can run in the background. |
| Permissions. | the owner of a file | Processes have owners too. |
| pgrep's cousin, grep. | grep -l | pgrep finds processes by name. |
By the end of this lesson you will be able to:
- say what a process and a PID are, and which commands start one;
- run commands in the background with &, and wait for them;
- keep track of jobs with jobs, %1 and their exit statuses;
- stop processes with kill, and choose between TERM, INT and KILL;
- pause, resume and move jobs with Ctrl+Z, bg and fg;
- find running processes with ps, pgrep and pkill.
Time on this page
Many blocks use sleep to stand for long work, such as an engine running. On this page, sleep costs no real time: the small shell counts the seconds instead of waiting for them, so a block that sleeps for an hour finishes at once, in the right order. In a real terminal, sleep 3600 waits a real hour.
In one line
This lesson is about programs while they run: seeing them, running them alongside your work, and stopping them, gently or firmly.
Programs that are running
At a glance
Think of a recipe and a meal being cooked. The recipe is written once and sits on the shelf, while cooking it starts, goes on for a while, and ends.
A program file is the recipe, and each run of it is a process, with a number called its process ID, or PID. The shell's own PID is in $$, and ps lists the processes of this terminal. Builtins such as cd run inside the shell itself, while programs start new processes, children of the shell.
Take a guess: Is there a folder somewhere for every running process?
Think of a recipe and a meal being cooked. The recipe is written once and sits on the shelf. Cooking it is an activity, which starts, goes on for a while, and ends, and two cooks can cook the same recipe at the same time. A program file is the recipe. Each time it runs, that run is a process.
A program while it runs. The kernel gives each process a number, its process ID or PID, and keeps track of it until it ends. When it ends, it leaves an exit status.
Your shell is a process too. The special name $$ holds its PID. The command ps, short for process status, lists the processes started from this terminal:
echo $$
ps
echo bye
5
PID TTY TIME CMD
5 pts/0 00:00:00 bash
8 pts/0 00:00:00 ps
bye
Read the table by its headings. PID is the number. TTY is the terminal it belongs to. TIME is how much processor time it has used so far, here almost none. CMD is the program's name. There are two: the shell itself, number 5, the same number that $$ printed, and ps, which is a process too while it looks.
Your numbers will be larger
The blocks on this page run in a small, fresh world, where the shell is process 5. They were checked against real bash in a fresh sandbox, which gives the same numbers. A real machine has started hundreds of processes before yours, so there you will see numbers such as 23514.
Which commands become processes
Not every command starts a process. A builtin, such as cd or echo, runs inside the shell itself. A program, such as sleep or ls, is a file that the kernel loads and runs as a new process, a child of the shell. The command type tells you which is which:
type cd
type echo
type sleep
cd is a shell builtin
echo is a shell builtin
sleep is /usr/bin/sleep
The shell that starts a process is its parent, and the new process is its child, the same words as for folders. When the child ends, the parent collects its exit status, which is how $? gets its number.
| Word | Means | Example |
|---|---|---|
| process | a program while it runs | sleep, running |
| PID | the process's number | $$ for the shell |
| parent | the process that started it | your shell |
| child | a process started by another | a command you run |
| builtin | a command run inside the shell, with no new process | cd, echo |
Worked example 1
Warden Rao asks two things: what number the shell has in this terminal, and whether sleep and echo each start a process of their own.
What we have: the special name $$, and the command type
What we need: the shell's PID, and which of the two commands is a program
The plan: echo $$ prints the shell's PID; type says whether a command is a builtin or a program file
echo $$
type echo
type sleep
5
echo is a shell builtin
sleep is /usr/bin/sleep
- echo is a builtin, so it runs inside the shell. sleep is a file in /usr/bin, so each run of it is a new process.
The shell is process 5; sleep starts a process, and echo does not.
Check: the ps table above lists bash as 5, and a sleep would appear there while it runs.
A path that fails: thinking that every command starts a process. cd could not work if it did: a child cannot change its parent's current folder.
When you are stuck: you cannot tell what is running
ps lists what runs from this terminal; the next sections show more ways.
$$ is this shell's own number.
type says whether a command is a builtin, which starts nothing, or a program file.
The numbers differ between machines; look at the names and the order, not the exact numbers.
Your turn
What is the difference between a program and a process?Tap to reveal
- A program is a file.
- A process is that program while it runs.
A program is the file; a process is one run of it, with its own PID.
Does cd start a process? Why not?Tap to reveal
- cd changes the shell's own current folder.
- A child process could not change its parent's folder.
No: it is a builtin, and must run inside the shell.
type ls
type pwd
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
ls is /usr/bin/ls
pwd is a shell builtin
- ls is a program in /usr/bin.
- pwd is built into bash.
It prints ls is /usr/bin/ls, then pwd is a shell builtin.
Did you know?
Every process has a folder
The kernel shows each running process as a folder. The Linux manual says /proc is a pseudo-filesystem, an interface to the kernel's own data. It holds a numbered folder for each running process, named by its PID. So ls /proc/$$ lists files that describe your own shell, though no disk stores them.
Why it fits here: It answers this topic's question: every running process has a folder in /proc, named by its PID.
Sources: Linux man-pages: proc(5); Linux man-pages: proc_pid(5)
PIDs wrap around
PIDs do not grow forever. The Linux manual says the file /proc/sys/kernel/pid_max holds the value at which PIDs wrap around, one more than the largest PID. Its default, 32,768, keeps the range of older kernels. On 64-bit systems it can be raised to 222, which is 41,94,304, and it also limits how many processes and threads can exist at once.
Why it fits here: It shows how far this topic's PIDs go: by default they wrap around after 32,768.
In one line
A process is a program while it runs, with a number called its PID; $$ is the shell's own. ps lists the processes of this terminal. Builtins run inside the shell; programs start new processes, children of the shell.
Running in the background
At a glance
Normally the shell waits: it starts a command and gives the prompt back only when the command ends. Put & at the end, and the shell starts it and returns at once.
The command then runs in the background while you carry on, like rice cooking while you chop vegetables. $! holds the PID of the last background job. Jobs finish in the order their work ends, not the order they started. The command wait waits for them all, and wait $! gives one job's exit status.
Take a guess: What happens to a finished job that nobody has waited for?
Normally the shell waits. It starts a command, the command runs in the foreground, and you get the prompt back only when it ends. Put & at the end of a command, and the shell starts it and returns at once. The command keeps running in the background, while you carry on.
It is like putting rice on to cook and chopping vegetables while it does. Here the background job sleeps for 2 seconds and then reports, while the foreground goes on working:
sleep 2 && echo slept &
echo working
wait
working
slept
The & belongs to the whole line before it, so sleep and its echo went into the background together. The shell printed working at once. Then wait made the shell wait until every background job had finished, so slept appeared before the block ended.
A command started with & at its end. The shell does not wait for it, and gives it a job number of its own. The special name $! holds the PID of the last one started.
sleep 30 &
echo $!
jobs
kill %1
8
[1]+ Running sleep 30 &
The sleep became process 8, and $! printed that number. The command jobs, explained in the next section, shows it running as job 1, and the last line stops it, which the signals section explains.
Order comes from time
Several jobs can run at once, and each finishes when its work is done, not in the order they were started:
sleep 3 && echo three &
sleep 1 && echo one &
echo now
wait
echo "all done"
now
one
three
all done
The job that slept 3 seconds started first, but the one that slept 1 second finished first. The foreground printed now before either of them.
wait, and a job's exit status
Given a PID, wait waits for that one process and then gives its exit status, as if it had run in the foreground:
false &
wait $!
echo $?
1
| Written | It does |
|---|---|
cmd & | starts cmd in the background, and returns at once |
$! | the PID of the last background job |
wait | waits for every background job |
wait $! | waits for that one, and gives its exit status |
Worked example 2
The morning pumps take a while to start: the north pump 2 seconds and the south pump 1 second. Start both in the background, check the gauges while they start, and report when both are running.
What we have: two pumps that take 2 and 1 seconds, and a check to do meanwhile
What we need: the gauges checked at once, each pump's report, and a last report after both
The plan: start each pump with &; do the check in the foreground; wait for both; then report
sleep 2 && echo "north pump done" &
sleep 1 && echo "south pump done" &
echo "checking gauges"
wait
echo "both pumps done"
checking gauges
south pump done
north pump done
both pumps done
- Nothing waited for the pumps until the wait line, so the gauges were checked first. The south pump reported first, as it needed less time.
The order is gauges, south, north, both done.
Check: the total time is 2 seconds, the longer pump's, not 3.
A path that fails: leaving out wait. The last echo would then print at once, before either pump had reported, and the report would be untrue.
When you are stuck: a background job does something unexpected
Its output appears in the middle of yours: that is normal, as both write to the same terminal.
The script ended before the job reported: add wait before the end.
You need its result: wait $! gives its exit status.
The & seems to apply to too much: it covers the whole line before it, joined with && or not.
Your turn
sleep 1 && echo late &
echo early
wait
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
early
late
- The sleep and its echo go into the background.
- The foreground prints early at once.
- wait lets late arrive before the end.
It prints early, then late.
What does $! hold?Tap to reveal
- It is set each time a job starts with &.
The PID of the last background job.
Two jobs start, one sleeping 5 seconds and one 2 seconds. Which reports first?Tap to reveal
- Each finishes when its own work is done.
The 2-second job.
Did you know?
& in the 1974 paper
Background jobs are as old as the Unix shell. The 1974 paper on Unix says that when a command is followed by &, the shell does not wait for it to finish before prompting again, and is ready at once for a new command. The & may be used several times in a line, so two commands can run side by side.
Why it fits here: It shows how old this topic's & is: the 1974 description of the Unix shell already ran commands in the background.
Source: Dennis M. Ritchie and Ken Thompson: The UNIX Time-Sharing System
A zombie until someone waits
A finished process does not vanish at once. The wait manual says a child that ends but has not been waited for becomes a zombie. The kernel keeps its PID, its exit status and how much it used, so that the parent can still collect them. Until a wait removes it, the zombie takes a slot in the process table, and ps marks it Z.
Why it fits here: It answers this topic's question: a finished job stays a zombie until its parent collects its status with a wait.
In one line
A command ended by & runs in the background, and the shell carries on at once; $! holds its PID. Jobs finish in the order their work ends. wait waits for them all, and wait $! gives one job's exit status.
Keeping track of jobs
At a glance
With several things in the background, you need a list. The command jobs shows every job of this shell, with its number, its state and its command.
A + marks the current job and a - the previous one. A job is named with a percent sign, so %1 means job 1, for kill, wait and fg. A job that has ended is reported once, as Done or as Exit with its status, and jobs -l adds each job's PID. Jobs belong to the shell that started them.
Take a guess: Can you name a job by its command instead of its number?
With several things in the background, you need a list. The command jobs shows every job of this shell, with its job number, its state, and the command:
sleep 30 &
sleep 40 &
jobs
kill %1
wait %1
echo $?
jobs
kill %2
[1]- Running sleep 30 &
[2]+ Running sleep 40 &
143
[2]+ Running sleep 40 &
Read a line from the left. [1] is the job number, which is small and belongs to this shell, unlike the PID. The + marks the current job, the most recent one, and the - marks the one before it. Then come the state and the command.
A job can be named by its number with a percent sign: %1 is job 1. Most commands that take a PID also take a job name, so kill %1 stopped the first sleep, and wait %1 collected its status, 143, which the next section explains. Afterwards, jobs listed only job 2, now the current job.
Finished jobs
A job that ends is reported once, the next time jobs runs, and then it leaves the list. A job that ended well says Done; one that failed says Exit and its status:
sleep 5 &
jobs
sleep 6
jobs
[1]+ Running sleep 5 &
[1]+ Done sleep 5
false &
sleep 1
jobs
[1]+ Exit 1 false
With -l, jobs adds each job's PID, so you can match the job numbers with ps:
sleep 30 &
sleep 3600 &
sleep 60 &
jobs -l
kill %1 %2 %3
[1] 8 Running sleep 30 &
[2]- 9 Running sleep 3600 &
[3]+ 10 Running sleep 60 &
| Name | Means |
|---|---|
%1 | job 1 |
%+ or %% | the current job, marked + |
%- | the previous job, marked - |
$! | the PID of the last job started |
Worked example 3
Two engines run in the background. Warden Rao wants to see both, stop the first, confirm that it stopped, and see what is still running.
What we have: two background sleeps, standing for the engines
What we need: the list before and after, and the stopped job's status
The plan: list with jobs; stop job 1 by name; wait %1 collects its status; list again
sleep 30 &
sleep 40 &
jobs
kill %1
wait %1
echo $?
jobs
kill %2
[1]- Running sleep 30 &
[2]+ Running sleep 40 &
143
[2]+ Running sleep 40 &
- The status 143 means that a signal ended the job. Only job 2 remains, and it is now the current job, marked +.
Job 1 stopped with status 143, and job 2 still runs.
Check: the second list has one line, for sleep 40.
A path that fails: writing kill 1 for job 1. A bare number is a PID, and PID 1 is not your sleep. The job's name needs the percent sign.
When you are stuck: jobs shows something unexpected
Nothing at all: no job is running from this shell. Jobs belong to the shell that started them.
Done or Exit: the job ended, and jobs reports that once.
A job number and a PID are different: %1 is a job, 8 is a PID. jobs -l shows both.
no such job: that job has ended and been collected, or never existed.
Your turn
sleep 9 &
sleep 8 &
jobs
kill %1 %2
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
[1]- Running sleep 9 &
[2]+ Running sleep 8 &
- Both jobs are still running.
- The second, most recent, is the current job: +.
It prints both jobs as Running, the second marked +.
What is the difference between %2 and 2 after kill?Tap to reveal
- The percent sign names a job.
- A bare number is a PID.
%2 is job 2; 2 is process 2.
What does jobs say about a job that ended with status 0?Tap to reveal
- A job that ended well is reported once.
Done, and then it leaves the list.
Did you know?
Job control from Austria
Job control began in the C shell. The tcsh manual credits J. E. Kulp of IIASA, in Laxenburg in Austria, with its job control features. Wikipedia adds that Jim Kulp's was the first job control, built on features of the 4.1BSD kernel. The KornShell, made at Bell Labs, adopted it, and the Bourne shell of SVR4 later did too.
Why it fits here: It shows where this topic's jobs, fg and bg came from: the C shell, with job control written at IIASA in Austria.
Sources: FreeBSD manual: tcsh(1); Wikipedia: Job control (Unix)
A job named by its command
A job can be named by its command as well as its number. The bash manual says %ce means the job whose command begins with ce, and %?ce any job with ce anywhere in its command line. A job named alone comes to the foreground: typing %1 does the same as fg %1. Typing %1 & does the same as bg %1.
Why it fits here: It answers this topic's question: %ce names a job by the start of its command, and %1 alone brings job 1 forward.
In one line
jobs lists this shell's jobs: number, + for the current and - for the previous, state and command. %1 names job 1 for kill and wait. A finished job shows Done, or Exit and its status, once; jobs -l adds the PIDs.
Signals: asking and telling
At a glance
To stop a running process, you send it a signal: a short message from the kernel, with a number and a name. Some signals ask, and one tells.
The command kill sends TERM, number 15, unless told otherwise, and TERM asks the process to end cleanly. INT, number 2, is what Ctrl+C sends. KILL, number 9, ends a process at once and cannot be caught. A process ended by a signal leaves the status 128 plus the signal's number. Ask with TERM before you tell with KILL.
Take a guess: When Linux shuts down, does it ask programs to end, or end them at once?
To stop a running process, you send it a signal: a short message from the kernel, with a number and a name. Some signals ask, and the process may finish what it is doing first. One signal tells, and cannot be refused. The command that sends signals has a fierce name, kill, but most of the time it only asks.
A numbered message that the kernel delivers to a process, such as TERM, number 15, which asks it to end. A process may catch most signals and decide what to do; it can never catch KILL, number 9.
| Signal | Number | Means | Sent by |
|---|---|---|---|
| TERM | 15 | please end, cleanly | kill, with no option |
| INT | 2 | interrupt | Ctrl+C |
| KILL | 9 | end now; cannot be caught | kill -9 |
| HUP | 1 | the terminal has gone | closing the terminal |
| TSTP | 20 | pause, from the keyboard | Ctrl+Z |
| CONT | 18 | carry on after a pause | fg and bg |
Ask first: TERM
With no option, kill sends TERM. A well-written program that receives TERM saves its work, closes its files, and ends. sleep has nothing to save, so it ends at once:
sleep 30 &
kill %1
wait %1
echo $?
143
Remember the rule from the first lesson: a process ended by a signal leaves the status 128 plus the signal's number. TERM is 15, so 128 + 15 = 143. Ctrl+C sends INT, number 2, which is why it gives 130:
sleep 30 &
kill -INT %1
wait %1
echo $?
130
Then tell: KILL
A program can catch TERM, and a stuck or badly written one may ignore it. Then you send KILL, with kill -9 or kill -KILL. The kernel ends the process at once, without asking it, so it gets no chance to save anything. The shell reports it:
sleep 30 &
kill -9 %1
wait %1
echo $?
bash: line 3: 8 Killed sleep 30
137
The report names the line where the shell noticed, the PID, and the command. The status is 128 + 9 = 137. Because KILL gives no chance to tidy up, a file being written can be left half-written. So always ask with TERM first, wait a moment, and tell with KILL only if the process will not go.
Names and numbers
kill -l lists every signal, and turns a number into a name, or a name into a number. Give it an exit status above 128, and it names the signal behind it. The option -0 sends nothing: it only checks that the process exists:
sleep 30 &
kill -l 9 15 2
kill -l 143
kill -0 %1
echo $?
kill %1
KILL
TERM
INT
TERM
0
Worked example 4
The drain engine from the scene has run all night, and two engines are running now. Find the drain engine, ask it to stop, and confirm from its exit status that a signal ended it.
What we have: two background jobs: the drain engine, sleep 3600, and a pump, sleep 60
What we need: the drain engine stopped, and its status explained
The plan: list the jobs to find the drain engine's number; send TERM with kill; collect the status with wait; turn the status into a signal's name with kill -l
sleep 3600 &
sleep 60 &
jobs
kill %1
wait %1
kill -l $?
jobs
kill %2
[1]- Running sleep 3600 &
[2]+ Running sleep 60 &
TERM
[2]+ Running sleep 60 &
- wait %1 left the status 143 in $?, and kill -l $? named it: TERM, the signal that asked. The pump, job 2, still runs.
The drain engine, job 1, ended by TERM.
Check: the second list shows only the pump.
A path that fails: starting with kill -9. It works here, but an engine that was writing its log would leave it half-written; ask with TERM first.
When you are stuck: a process will not stop
First ask: kill PID, or kill %1, which sends TERM.
Wait a moment, then check with kill -0 PID, or with ps.
Only then tell: kill -9 PID. It cannot be refused.
Operation not permitted: the process belongs to another user; only its owner, or root, may signal it.
Your turn
A job ended with status 137. Which signal ended it?Tap to reveal
- Subtract 128.
- 9 is KILL.
KILL, number 9.
Why should you try kill before kill -9?Tap to reveal
- TERM lets the program tidy up.
- KILL ends it at once, with no chance to save.
Because TERM lets the program finish cleanly; KILL does not.
sleep 30 &
kill %1
wait %1
echo $?
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
143
- kill sends TERM, number 15.
- 128 + 15 = 143.
It prints 143.
Did you know?
Ask, wait 90 seconds, then tell
Linux itself asks before it tells. When systemd, the system and service manager, stops a service, its manual says the processes are first sent SIGTERM. If any are still there once the stop timeout has passed, the request is repeated with SIGKILL. That timeout is 90 seconds unless set otherwise, so a stuck program can hold up a shutdown for a minute and a half.
Why it fits here: It answers this topic's question: at shutdown Linux asks with TERM, waits up to 90 seconds, and only then tells with KILL.
Sources: systemd manual: systemd(1); systemd manual: systemd.kill(5); systemd manual: systemd-system.conf(5)
Names travel, numbers do not
Signal names are safer than numbers. The signal manual says many signals have different numbers on different kinds of processor. TSTP, the pause sent by Ctrl+Z, is 20 on x86 and ARM, 18 on Alpha and SPARC, and 24 on MIPS. So kill -TSTP is right on every machine, while kill -20 is right only on some.
Why it fits here: It adds care to this topic's kill: written by name, a signal means the same on every kind of machine.
Source: Linux man-pages: signal(7)
In one line
kill sends a signal: TERM, 15, by default, which asks a process to end; INT, 2, is Ctrl+C; KILL, 9, ends it at once and cannot be caught. A process ended by a signal leaves 128 plus its number. Ask with TERM before you tell with KILL.
Pausing and resuming at the keyboard
At a glance
In a real terminal you can move a job between the foreground and the background while it runs. This is called job control, and it needs three keys and commands.
Ctrl+Z pauses the job in the foreground, bg lets a paused job carry on in the background, and fg brings a job back to the foreground. Ctrl+C ends the foreground job instead. News of a job that changed state appears at the next prompt. So a long command never has to be started again. Scripts have no job control.
Take a guess: What should you press when the terminal suddenly shows nothing you type?
In a real terminal you can also move a job between the foreground and the background while it runs. This is called job control, and it works with three keys and commands: Ctrl+Z pauses the job in the foreground, bg lets a paused job carry on in the background, and fg brings a job back to the foreground.
Suppose you start a long command, and then realise that you need your prompt. You do not have to stop it and start again. This session was typed in a real terminal:
Typed: sleep 100, Ctrl+Z, jobs, bg, jobs, fg, then Ctrl+C
asha@tower:~/shop$ sleep 100
^Z
[1]+ Stopped sleep 100
asha@tower:~/shop$ jobs
[1]+ Stopped sleep 100
asha@tower:~/shop$ bg
[1]+ sleep 100 &
asha@tower:~/shop$ jobs
[1]+ Running sleep 100 &
asha@tower:~/shop$ fg
sleep 100
^C
asha@tower:~/shop$ echo $?
130
asha@tower:~/shop$
Follow it line by line. Ctrl+Z, shown as ^Z, paused sleep and gave the prompt back: the job is Stopped, which means paused, not ended. The command bg let it run on in the background, as if it had been started with &. The command fg brought it back to the foreground, and printed its command, so the shell was waiting for it again. Ctrl+C then ended it with INT, status 130.
| You press or type | It does | The job is then |
|---|---|---|
| Ctrl+C | sends INT to the foreground job | ended |
| Ctrl+Z | sends TSTP: pauses the foreground job | Stopped |
bg | lets a stopped job run on, in the background | Running |
fg | brings a job to the foreground | running, and the shell waits |
fg %2 | brings job 2 to the foreground | running, in the foreground |
Reports at the next prompt
An interactive shell reports a background job's end when it next shows the prompt. It also prints the job number and PID when a job starts. Here the PID was 701458; yours will be different:
Typed: sleep 100 &, jobs, kill %1, Enter, then echo done
asha@tower:~/shop$ sleep 100 &
[1] 701458
asha@tower:~/shop$ jobs
[1]+ Running sleep 100 &
asha@tower:~/shop$ kill %1
asha@tower:~/shop$
[1]+ Terminated sleep 100
asha@tower:~/shop$ echo done
done
asha@tower:~/shop$
No job control in scripts
Job control belongs to a person at a terminal. A script has no keyboard, so bash switches job control off when it runs one, and fg and bg refuse. The blocks on this page run as scripts, which is why they never show these keys:
sleep 30 &
fg
kill %1
bash: line 2: fg: no job control
Worked example 5
Corin started a long copy in the foreground, and now needs his prompt to check a gauge, without stopping the copy. Show the keys and commands, in order.
What we have: a long job in the foreground, and the keyboard
What we need: the prompt back, with the job still running
The plan: pause it with Ctrl+Z, then let it run on in the background with bg; check that it runs with jobs
- Ctrl+Z pauses the copy: [1]+ Stopped.
- bg lets it carry on in the background: [1]+ ... &.
- jobs confirms: [1]+ Running.
- The prompt is free, and the copy runs on. fg brings it back whenever he likes.
Ctrl+Z, then bg.
Check: after bg, jobs says Running, not Stopped.
A path that fails: Ctrl+Z alone. The prompt comes back, but the job is only paused: Stopped. Without bg, the copy makes no progress at all.
When you are stuck: a job seems to have frozen
jobs says Stopped: it was paused with Ctrl+Z. Use bg or fg.
Pressing Ctrl+C did nothing to a background job: Ctrl+C only reaches the foreground. Use kill %1.
fg: no job control: you are in a script, or on this page; job control needs a real terminal.
You closed the terminal and the job stopped: closing sends HUP. Start such jobs with nohup, or in a tool built for it.
Your turn
What is the difference between Ctrl+C and Ctrl+Z?Tap to reveal
- Ctrl+C sends INT, which ends the job.
- Ctrl+Z sends TSTP, which pauses it.
Ctrl+C ends the foreground job; Ctrl+Z pauses it.
A job is Stopped. Which command lets it carry on without taking over your terminal?Tap to reveal
- fg would take the foreground.
- bg runs it in the background.
bg.
Why does fg fail in a script?Tap to reveal
- Job control is for a person at a terminal.
- bash switches it off in scripts.
Because scripts have no job control.
Did you know?
A frozen screen is often Ctrl+S
A terminal that seems frozen may only be paused. The termios manual says Ctrl+S is the Stop character, which stops output until the Start character is typed, and that Ctrl+Q is the Start character. It lists them by their old ASCII names too, DC3 and DC1. So if typing seems to do nothing after an accidental Ctrl+S, press Ctrl+Q, and the output carries on.
Why it fits here: It answers this topic's question: Ctrl+S pauses a terminal's output, and Ctrl+Q lets it carry on.
Source: Linux man-pages: termios(3)
Ctrl+Z asks, STOP tells
Pausing has an ask and a tell too. The termios manual says Ctrl+Z sends SIGTSTP, a signal a program may catch and handle in its own way. The signal manual says SIGSTOP, like SIGKILL, cannot be caught, blocked or ignored. So kill -STOP pauses any process you may signal, even one that ignores Ctrl+Z, and kill -CONT lets it continue.
Why it fits here: It mirrors this topic's Ctrl+Z: TSTP asks a program to pause, while STOP pauses it whatever it does.
Sources: Linux man-pages: termios(3); Linux man-pages: signal(7)
In one line
In a real terminal, Ctrl+Z pauses the foreground job, bg lets it run on in the background, and fg brings a job back to the foreground; Ctrl+C ends it. Scripts, and this page, have no job control.
Finding processes: ps, pgrep and top
At a glance
The command jobs only knows this shell's own jobs. To find a process that anything started, even another person's, you need tools that ask the kernel.
With no options, ps lists the processes of this terminal, and ps aux lists every process on the machine. The command pgrep finds PIDs by name, and pkill signals every match, so look first. The command top is a live view of the busiest processes, left with q, and nohup starts a command that can outlive the terminal.
Take a guess: Why is the signal for a closed terminal called a hang-up?
jobs only knows this shell's own jobs. To find a process that anything started, even another person's, you need tools that ask the kernel. Here are three, from the simplest up.
ps
With no options, ps lists the processes of this terminal. Background jobs are there too, with their PIDs:
sleep 30 &
sleep 60 &
ps
kill %1 %2
PID TTY TIME CMD
5 pts/0 00:00:00 bash
8 pts/0 00:00:00 sleep
9 pts/0 00:00:00 sleep
10 pts/0 00:00:00 ps
To see every process on the whole machine, the manual of ps gives ps ax, and ps aux for a fuller table. The a means other users' processes too, the x means those with no terminal, such as servers, and the u adds the owner, and how much processor and memory each one uses. The small shell has only plain ps, so try these in a real terminal:
In a real terminal: the first lines of every process on the machine
ps aux | head -n 5
On a real machine that list is long. Pipe it into grep to find a name, as the pipes lesson taught, or use pgrep.
pgrep and pkill
The command pgrep prints the PIDs of the processes whose names match a pattern; with -l it adds the name. pkill sends a signal, TERM unless you say otherwise, to every process that matches, so look with pgrep first:
sleep 30 &
pgrep -l sleep
pkill sleep
wait $!
echo $?
8 sleep
143
top
The command top is a live view of the whole machine: the busiest processes, with their PIDs, owners, and use of the processor and memory, redrawn over and over. It takes over the window until you press q. That makes it the first thing to open when a machine is slow:
In a real terminal: a live view of what is running; q leaves
top
A job that must outlive the terminal
Closing a terminal sends HUP to its jobs, and they usually end. The command nohup starts a command with HUP ignored, so that it can go on after you log out; its output goes to a file named nohup.out. Servers that must run for months are started by the system instead, which a later step of this school teaches.
| You want | Use |
|---|---|
| this terminal's processes | ps |
| every process on the machine | ps aux |
| the PIDs of processes with a name | pgrep name, or pgrep -l name |
| to send TERM to them all | pkill name |
| a live view of the busiest | top, then q |
| a job that survives logging out | nohup command & |
Worked example 6
Three engines run, and Warden Rao names the one that burns coal all night: the one started with sleep 3600. Find its PID, stop it by that number, and confirm the stop.
What we have: three background jobs, and the commands jobs -l, kill and wait
What we need: the right PID, stopped, with its status
The plan: jobs -l shows each job's PID beside its command, so the drain engine's number can be read off; kill and wait take that PID
sleep 30 &
sleep 3600 &
sleep 60 &
jobs -l
kill 9
wait 9
echo $?
kill %1 %3
[1] 8 Running sleep 30 &
[2]- 9 Running sleep 3600 &
[3]+ 10 Running sleep 60 &
143
- The line for sleep 3600 shows PID 9, so kill 9 asked exactly that process to stop, and wait 9 collected its 143. The last line stops the other two, to leave things tidy.
The drain engine was PID 9, and it ended with 143, by TERM.
Check: 143 is 128 + 15, and 15 is TERM.
A path that fails: pkill sleep. It matches by name, and all three engines are named sleep, so it stops the two that were doing useful work as well.
When you are stuck: you cannot find the process you want
ps shows only this terminal's; use ps aux for the whole machine.
Too many lines: pipe ps aux into grep, or use pgrep -l name.
Several processes share the name: compare their arguments or PIDs before you kill any.
The machine is slow and you do not know why: open top, and look at the top lines.
Your turn
Which command prints the PIDs of every process named sleep?Tap to reveal
- pgrep matches names.
pgrep sleep.
What is dangerous about pkill?Tap to reveal
- It signals every process whose name matches.
It may stop more processes than you meant; look with pgrep first.
sleep 30 &
pgrep -l sleep
kill %1
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
8 sleep
- The background sleep is process 8.
- -l adds its name.
It prints 8 sleep.
Did you know?
ps aux is not ps -aux
ps accepts three kinds of options: Unix ones with a dash, BSD ones without, and GNU long ones with two dashes. That is why ps aux works with no dash at all. The manual warns that ps -aux mixes two standards. By those standards it asks for processes with a terminal plus those of a user named x, and only if no such user exists does ps guess that ps aux was meant.
Why it fits here: It sharpens this topic's ps aux: without a dash it is a BSD option, and with one it can mean a user named x.
Source: Linux man-pages: ps(1)
A hang-up from the modem days
The signal HUP is named after hanging up a line. The signal manual still describes it as a hangup detected on the controlling terminal, or the death of the controlling process. The termios manual keeps a setting that lowers the modem control lines, a hang-up, when the last process closes the terminal. The command nohup runs a command immune to hangups, with that signal ignored.
Why it fits here: It answers this topic's question: HUP is a hang-up, named from terminals on modem lines, and nohup ignores it.
Sources: Linux man-pages: signal(7); Linux man-pages: termios(3); GNU Coreutils manual: nohup invocation
In one line
ps lists this terminal's processes, and ps aux the whole machine's. pgrep finds PIDs by name, and pkill signals every match, so look first. top is a live view, left with q, and nohup lets a job outlive the terminal.
Where it is used
Processes are what a server is made of. Here is where each idea of this lesson does its work.
| What you learned | Where it is used | A real example |
|---|---|---|
| PIDs and ps | finding what runs | the web server's processes on a server |
& and wait | several jobs at once | three downloads started together |
| exit statuses | knowing how a job ended | a test run stopped from outside, 143 |
kill, then kill -9 | stopping what is stuck | a program that stopped answering |
| Ctrl+Z, bg, fg | keeping the prompt free | a long copy moved to the background |
top | a slow machine | one program using all the processor |
pgrep, pkill | processes by name | every copy of a stuck program |
nohup | a job after logging out | a long job started over a network |
Mistakes and debugging
These are the slips that people meet most often with processes. Every output below is the real one, from the code shown.
1. Forgetting wait
sleep 1 && echo "pump ready" &
echo "all set"
all set
The script ended before the job could report, so pump ready never appears here. The fix: wait before the end.
2. A process that has already ended
sleep 1 &
wait
kill $!
echo $?
bash: line 3: kill: (8) - No such process
1
The job finished before kill was sent, so its PID no longer names anything. On a real machine the number may even be given to a new process later. The fix: check first with jobs, ps or kill -0.
3. Waiting for a job that has gone
sleep 1 &
wait
wait %1
bash: line 3: wait: %1: no such job
The first wait collected the job, so it is no longer in the list. The fix: keep the status when you first wait, as in wait %1; s=$?.
4. fg in a script
sleep 30 &
fg
kill %1
bash: line 2: fg: no job control
Job control needs a person at a terminal. The fix: in a script, use wait.
5. Going straight to kill -9
sleep 30 &
kill -9 %1
wait %1
bash: line 3: 8 Killed sleep 30
It works, but the process had no chance to save its work. The fix: kill %1 first, and -9 only if it will not go.
The debugging corner: what runs, and how did it end?
When a job misbehaves, ask two questions. What runs now? Ask jobs -l for your own jobs, ps or pgrep -l name for the rest. How did it end? Collect its status with wait, and turn a number above 128 into a signal's name with kill -l $?.
Quick card
The whole lesson on one card. Come back to it whenever you need a reminder.
The lesson in code
echo $$ # this shell's PID
sleep 3600 & # start in the background
echo $! # its PID
sleep 1 && echo done & # another job
jobs # list the jobs
kill %1 # ask job 1 to end (TERM)
wait %1; echo $? # collect it: 128 + 15
wait # wait for the rest
kill -l 143 # which signal was that?
5
8
[1]- Running sleep 3600 &
[2]+ Running sleep 1 && echo done &
143
done
TERM
| When you see | It means | Look at |
|---|---|---|
| status 130 | INT ended it: Ctrl+C | nothing to fix |
| status 143 | TERM ended it: kill | who sent it |
| status 137, and Killed | KILL ended it: kill -9 | why it would not stop |
Stopped | paused by Ctrl+Z | bg or fg |
no such job | the job has ended and been collected | jobs |
No such process | that PID is not running | ps, pgrep |
Operation not permitted | the process is not yours | its owner, or sudo |
fg: no job control | a script, not a terminal | wait |
The story: Ask, then tell
You read the list of what is running, with the number beside each and how long it has burned. You find the drain engine at the top, send it the signal that asks it to finish and stop, and it ignores you, so you send the one that cannot be ignored. Then you start the morning's pumps behind you, so that they run while you carry on with your work.
The coal gauge steadies, and the other engines pick up their speed.
Warden Rao writes the number in the book. "Next time," she tells the tunnel at large, "leave a name with it."
Reagents gathered: process, PID, background job, signal, kill, foreground.
Sources
- GNU Bash Reference Manual (GNU Free Documentation License 1.3 or later (the manual's copying notice, read 2026-11-06); facts and ideas only)
- GNU Coreutils manual (GNU Free Documentation License 1.3 or later (the manual's copying notice, read 2026-11-06); facts and ideas only)