Linux Processes and Signals: ps, top and kill
Your laptop fan is roaring because a script you started in another terminal window is eating the CPU. On a server there are no windows to close. This lesson covers processes and signals on Linux: how to list what is running with ps and top, and how to stop, pause or reload a program with kill, politely first.
Listing processes with ps aux and top
A process is a running program with its own memory and a process ID (PID), as lesson 3 explained. ps, short for process status, prints a snapshot of the processes running now.
In ps aux, a adds other users' processes, x adds those with no terminal, such as background programs, and u picks columns for the owner and resource use. The list runs to hundreds of lines, so pipe it into grep (lesson 9) to find one program:
ps aux | grep sleep
With a sleep 300 running, Linux prints something like this (numbers are illustrative):
asha 4321 0.0 0.0 5772 1024 pts/0 S 10:41 0:00 sleep 300
asha 4402 0.0 0.0 6612 2176 pts/0 S+ 10:45 0:00 grep --color=auto sleep
The second line is grep itself, caught in ps's snapshot. If only that line appears, no sleep is running. pgrep sleep prints just the matching PIDs and never lists itself.
The diagram labels every column. Notice STAT: one letter says whether a process is running, sleeping, stopped or a zombie.
PID is the number you hand to kill. RSS is the RAM a process really occupies, in kibibytes (KiB, units of 1,024 bytes); VSZ also counts memory it has reserved but not loaded, so it is larger. In STAT, a lowercase s marks a session leader, usually the shell that owns the terminal. macOS shows TT and STARTED as headings and writes U where Linux writes D.
One trap: on Linux, ps's %CPU is an average over the process's whole life, not this second. For a live view, run top, which keeps refreshing with the busiest processes first; press q to quit. htop is a friendlier version you may need to install, and lesson 14 reads their columns in detail.
What a signal is
A signal is a small, numbered message that the kernel, the core of the operating system (lesson 2), delivers to a process. You ask for one with the kill command, and keys such as Ctrl+C send one to the program in front.
Each signal has a default action, usually ending the process. A program can instead catch a signal, running its own code when it arrives, or ignore it. Two cannot be caught, blocked or ignored: SIGKILL, and SIGSTOP, a pause that cannot be refused.
In the table, notice the last column: SIGTERM and SIGINT ask, while SIGKILL forces. The numbers 1, 2, 9 and 15 match on Linux and macOS; some others differ, so use names when in doubt.
SIGHUP, short for hang-up, dates from terminals on phone lines. Closing a terminal window still sends it to the shell, which passes it on to the jobs it started. Services, programs that run in the background with no terminal, such as a web server, often reuse SIGHUP to mean "reload your settings": OpenSSH's server, sshd, rereads its configuration file when it gets one.
Stopping a process: kill, kill -9 and pkill
Despite its name, kill only sends a signal; given just a PID, it sends SIGTERM, the polite request. Stop the harmless sleep from the listing, which would otherwise wait 300 seconds and exit, then look again:
kill 4321
ps aux | grep sleep
kill prints nothing when it works, and only the grep line is left:
asha 4410 0.0 0.0 6612 2176 pts/0 S+ 10:46 0:00 grep --color=auto sleep
A wrong or old PID gives bash: kill: (4321) - No such process; zsh, the macOS default, words it differently. Operation not permitted means another user owns the process: you may signal only your own without sudo (lesson 7).
Why not always force it? SIGTERM gives the program a chance to clean up: a well-written one catches it, finishes the file it is writing, flushes its buffers (writes out data still waiting in memory) and deletes its temporary and lock files, the markers that say it is running. SIGKILL ends it between two instructions: the kernel frees its memory, but unwritten data is lost, and a leftover lock file can stop it starting again.
So follow the flowchart: SIGTERM, wait a few seconds, and only then kill -9 4321, which you can also write kill -KILL 4321. Notice the side note: systemctl stop (lesson 12) and docker stop run the same steps for you.
pkill signals every process whose name matches a pattern, a regular expression as in lesson 9, so pkill sleep would also hit a program called sleepwatch. Preview first with pgrep -l, which lists the PID and name of each match:
pgrep -l sleep
pkill sleep
pgrep prints a line like 4321 sleep. pkill then sends SIGTERM and prints nothing, unless you pick another signal, as in pkill -KILL sleep. If pgrep finds nothing, check the spelling: on Linux it matches only the first 15 characters of a name, and -f matches the whole command line instead.
Foreground and background jobs
A job is a command started from your shell (lesson 4). A foreground job owns the terminal until it ends, and Ctrl+C sends it SIGINT. End a command with & to run it in the background: the prompt returns at once, and the shell prints the job number and the PID.
jobs lists your jobs, fg brings one to the foreground, Ctrl+Z pauses the foreground job with SIGTSTP, and bg resumes a paused job in the background. %1 means job 1. In this bash session, you type the lines after $:
$ sleep 300 &
[1] 4500
$ jobs
[1]+ Running sleep 300 &
$ fg
sleep 300
^Z
[1]+ Stopped sleep 300
$ bg
[1]+ sleep 300 &
$ kill %1
[1]+ Terminated sleep 300
The Terminated line may appear only after you press Enter. zsh words these lines differently, such as suspended for Stopped. Work that must outlive your terminal belongs in a service (lesson 12).
Zombies, and processes that will not die
When a process ends, the kernel keeps its exit status until its parent, the process that started it, collects it. Until then it is a zombie: STAT Z, with <defunct> after its name. It has already ended, so it uses no CPU or memory, only a slot in the kernel's process table, its list of processes.
So kill -9 does nothing to a zombie. A few are harmless. Hundreds mean the parent never collects its children, and since the table has a limit, enough can block new processes. Fix or end the parent: its orphans are adopted, usually by PID 1, which collects them.
When a process seems to survive kill -9, there are four usual reasons:
- It is a zombie: deal with its parent.
- Its STAT is D: it is waiting inside the kernel, often on a slow disk or network drive, and ends only when that wait does.
- kill said
Operation not permitted, so no signal was sent. - It returns with a new PID: a service manager, such as systemd, restarts it, so stop it there.
Check yourself
A database is still running two seconds after kill 812. Why wait a little longer before kill -9?
It may be handling SIGTERM: writing data to disk and closing files. SIGKILL would stop it mid-write, losing unsaved data or leaving damaged files.
ps shows a process with STAT Z, and kill -9 changes nothing. Why, and what would remove it?
It has already ended; only its exit status is left for its parent to collect. Make the parent collect it, or end the parent: the zombie is then adopted, usually by PID 1, which collects it.
You start a long backup with & and close the terminal window. Why does the backup probably stop?
Closing the terminal sends SIGHUP to the shell, which passes it on to its jobs, and SIGHUP's default action ends a process.