Creating, monitoring and killing processes

Seeing what is running and making it stop: ps and top, foreground and background jobs, the signals kill actually sends, and how to keep a process alive after you log out.

Lesson 5 of 8 in objective 103. GNU and Unix commands, part of LPIC-1 Exam 101-500.

The two signals worth knowing by number. SIGTERM — Number: 15, and the default; Can it be caught?: Yes — or ignored; The process gets to: Flush files and exit cleanly; Reach for it: Always first. SIGKILL — Number: 9; Can it be caught?: No. The kernel enforces it; The process gets to: Do nothing at all; Reach for it: Only when 15 failed SIGTERM SIGKILL Number 15, and the default 9 Can it be caught? Yes — or ignored No. The kernel enforces it The process gets to Flush files and exit cleanly Do nothing at all Reach for it Always first Only when 15 failed
The two signals worth knowing by number.

Looking at processes

ps has two incompatible option styles and both are in use. ps aux is the BSD spelling and ps -ef the UNIX one; both list every process on the system with its owner, PID and command. ps alone shows only your own processes on this terminal, which surprises people. The parent PID column is what shows the tree, and pstree draws it directly.

top is the live view, refreshing every few seconds and sortable interactively; it also shows load average, memory and per-process CPU. uptime prints just the load averages — one, five and fifteen minutes — and free prints memory, where the number that matters is "available" rather than "free", because Linux deliberately uses spare memory as cache. watch runs any command repeatedly so you can watch its output change.

Jobs, foreground and background

Appending & starts a command in the background and the shell prints its job number and PID. Ctrl-Z suspends the foreground job, bg resumes it in the background, fg brings it back, and jobs lists what this shell is managing. Job numbers are per-shell and are referred to as %1, %2 — they are not PIDs, and confusing the two is a reliable exam trap.

A background job still dies when its shell exits, because the shell sends it a hangup. nohup runs a command immune to that signal and redirects its output to nohup.out; disown removes an already-running job from the shell's table. A terminal multiplexer — screen or tmux — is the fuller answer: the session keeps running on the machine and you reattach to it later.

Where Ctrl-Z leaves a job, and the two states fg brings it back from. A column of 4 states: Not started; In the foreground; Stopped (Listed by jobs as %1, which is not a PID); In the background (Still dies when its shell exits; the shell sends a hangup). You get from Not started to In the foreground by the command, typed plain; from In the foreground to Stopped by Ctrl-Z; from Stopped to In the background by bg %1; from Not started to In the background by the command, with & appended; from Stopped to In the foreground by fg %1; from In the background to In the foreground by fg %1. One arrow is drawn crossed through, because that move does not exist: In the foreground to In the background — never: bg resumes a suspended job, so Ctrl-Z comes first. Not started the command, typed plain In the foreground Ctrl-Z Stopped Listed by jobs as %1, which is not a PID bg %1 In the background Still dies when its shell exits; the shell sends a hangup fg %1 the command, with & appended never: bg resumes a suspended job, so Ctrl-Z comes first fg %1
Where Ctrl-Z leaves a job, and the two states fg brings it back from.

Signals, and what kill really does

kill sends a signal; it does not necessarily kill anything. The default is SIGTERM (15), which asks a process to shut down and can be caught, so the process can clean up. SIGKILL (9) cannot be caught or ignored and is enforced by the kernel, which is why it is the last resort rather than the first: a process killed with 9 gets no chance to flush its files. SIGHUP (1) means the terminal went away, and many daemons repurpose it to mean "reload your configuration". Ctrl-C sends SIGINT (2).

kill takes a PID. killall takes a command name and signals every matching process. pkill matches on patterns and attributes, and pgrep does the same match but only prints the PIDs. kill -l lists the signal names and numbers.

Worth carrying in

ps aux
Every process, BSD style. ps -ef is the equivalent UNIX spelling.
pstree
The process tree, showing parentage directly.
jobs / fg %1 / bg %1
Shell job control. Job numbers are not PIDs.
kill -15 PID
SIGTERM: ask politely. The default.
kill -9 PID
SIGKILL: cannot be caught, no cleanup.
kill -1 PID
SIGHUP: terminal gone, or "reload your configuration" by convention.
killall name
Signal every process with that command name.
pgrep / pkill
Find or signal processes by pattern and attribute.
nohup cmd &
Immune to hangup; output to nohup.out.
free -h
Memory. Read "available", not "free".

What the exam does with this

Objective
103. GNU and Unix commands
Share of the exam
43.33% (the whole objective)
Questions in this lesson
20
Signed for by a person
0

Partly checked. None of the 20 questions here has been read against the cited source by a person. 20 questions have been checked against their cited clause by an automated pass — which is not the same thing, and is not a signature.

Only questions a person has signed for are used in mock exams here. That is the whole difference between the two kinds of checking above.

How these questions are written — where each question comes from, what the verification ledger records, and what happens when one is found wrong.

Drill this lesson

A lesson is one sitting: the trainer draws a short run from these questions alone and spaces the ones you get wrong.

Practise Creating, monitoring and killing processes

Questions in this lesson

Practise Creating, monitoring and killing processes

The rest of objective 103