Processes, Tuning and LogsLesson 1 of 3
Finding and stopping processes
Read the process list, find what is eating CPU or memory, and stop it with the gentlest signal that works.
pstoppgreppkillkillkillallkill -ljobsps --sortfree
After this lesson you can
- List processes with ps and top and sort them by CPU or memory use.
- Find a process by name or owner with pgrep, and stop it with kill or pkill using the correct signal.
- Explain SIGTERM, SIGKILL and SIGHUP, and what states like S, R, D and Z mean.
The idea
A process is a running program with a PID (process ID), an owner, a parent, a state, and resource usage. Administrators spend a good deal of time answering two questions: what is using the machine? and how do I make it stop?
Seeing processes
| Tool | Use |
|---|---|
ps -ef or ps aux | A snapshot of every process |
ps -eo pid,user,%cpu,%mem,comm --sort=-%cpu | You choose the columns and sort order |
top | A live, refreshing view; P sort by CPU, M by memory, k kill, q quit |
pgrep -a name | Find PIDs by name, with the command line |
free -h | Memory and swap totals |
uptime | Load averages over 1, 5 and 15 minutes |
The load average is the average number of processes wanting CPU or waiting on disk. As a rough rule, a sustained load much higher than the number of CPU cores (nproc) means the machine is overloaded.
The state letter in STAT: R running or ready, S sleeping (waiting for something, normal), D uninterruptible sleep (usually disk or NFS I/O, cannot be killed until the I/O returns), T stopped, Z zombie (already finished, waiting for its parent to collect its exit status).
Signals
Stopping a process is really sending it a signal, and the program decides what to do, except for two.
| Signal | Number | Meaning |
|---|---|---|
SIGTERM | 15 | “Please terminate.” The default of kill. Lets the program clean up |
SIGKILL | 9 | Immediate, from the kernel. Cannot be caught or ignored; no cleanup |
SIGHUP | 1 | “Hang up.” Many daemons re-read their configuration on it |
SIGSTOP / SIGCONT | 19 / 18 | Pause and resume |
SIGINT | 2 | What Ctrl+C sends |
Always try SIGTERM first and give it a few seconds. Use -9 only if the process ignores it, because a killed process cannot remove its lock files, flush buffers or tell its children.
kill 2214 # SIGTERM by PID
kill -9 2214 # SIGKILL
pkill sha256sum # by name (a pattern matched against the command name)
pkill -u alex # everything owned by alex
killall httpd # all processes with this name
Try it
ps -eo pid,user,%cpu,%mem,comm --sort=-%cpu | head -6
PID USER %CPU %MEM COMMAND
2214 alex 97.3 0.0 sha256sum
842 root 0.2 0.4 sshd
731 root 0.1 0.6 tuned
sha256sum /dev/zero &
pgrep -a sha256sum
kill $(pgrep sha256sum)
[1] 2214
2214 sha256sum /dev/zero
[1]+ Terminated sha256sum /dev/zero
top -b -n1 | head -5
top - 11:20:41 up 3:40, 1 user, load average: 0.95, 0.40, 0.18
Tasks: 128 total, 2 running, 126 sleeping, 0 stopped, 0 zombie
%Cpu(s): 25.0 us, 0.4 sy, 0.0 ni, 74.6 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 1740.4 total, 812.6 free, 461.2 used, 599.1 buff/cache
MiB Swap: 2048.0 total, 2048.0 free, 0.0 used. 1279.2 avail Mem
The header tells you most of what you need: load average, how many processes exist per state, the CPU split (us user, sy kernel, id idle, wa waiting on I/O) and memory. A high wa points at storage, not CPU. Read avail Mem, not free: Linux uses spare memory for the file cache, which is returned to programs when needed.
Ignoring SIGTERM
bash -c 'trap "" TERM; sleep 600' &
kill %1; sleep 1; jobs
kill -9 %1
[1] 2301
[1]+ Running bash -c 'trap "" TERM; sleep 600' &
[1]+ Killed bash -c 'trap "" TERM; sleep 600'
By user, by name
pgrep -u alex -a
sudo pkill -u alex sleep
Going further
kill -l lists every signal. renice and nice (next lesson) change priority instead of stopping. systemctl kill --signal=SIGHUP SERVICE signals a unit’s processes, and for a service you should stop it with systemctl stop, not kill, or systemd will restart it. lsof -p PID and ls -l /proc/PID/fd show what a process has open, strace -p PID shows its system calls, and cat /proc/PID/status shows memory details. Out-of-memory kills appear in the journal as Out of memory: Killed process.
Practice
0 of 5 done
Do these on your own system. They get harder. Check each result, then tick it off.
- Warm-up
Who is using the CPU and memory?
Print the five processes using the most CPU, then the five using the most memory, each with PID, owner, CPU%, memory% and command name.
Check your result
Two lists of five lines (plus a header) sorted descending by the chosen column.
Hint
ps -eo pid,user,%cpu,%mem,comm --sort=-%cpu | head -6. The minus sign sorts descending.Solution
ps -eo pid,user,%cpu,%mem,comm --sort=-%cpu | head -6 ps -eo pid,user,%cpu,%mem,comm --sort=-%mem | head -6 - Practice
Watch it live
Start a CPU hog in the background with
sha256sum /dev/zero &. Intop, find it, sort by memory withMand back to CPU withP, show a single-process view withtop -p PID, then quit. Record the PID you will need to kill.Check your result
topshowssha256sumnear 100 % CPU.pgrep -a sha256sumprints its PID.Hint
top -b -n1 | head -15gives a one-shot, non-interactive view.qquits interactivetop.Solution
sha256sum /dev/zero & pgrep -a sha256sum top -b -n1 | head -12 - Practice
Stop it politely, then forcefully
Stop the
sha256sumprocess by PID using the default signal, confirm it is gone. Then start another one, stop it by name with a single command, and finally demonstrate when-9is needed using a process that ignoresSIGTERM(create one withbash -c 'trap "" TERM; sleep 600' &).Check your result
After
kill PIDthe process is gone frompgrep.pkill sha256sumdoes the same by name.kill PIDdoes nothing to the trap-ing shell, andkill -9 PIDremoves it.Hint
kill PIDsends SIGTERM (15).kill -9sends SIGKILL, which cannot be ignored. Always try 15 first, so the program can clean up.Solution
sha256sum /dev/zero & pid=$! kill $pid; sleep 1; pgrep sha256sum || echo gone sha256sum /dev/zero & pkill sha256sum; sleep 1; pgrep sha256sum || echo gone bash -c 'trap "" TERM; sleep 600' & pid=$! kill $pid; sleep 1; ps -p $pid -o pid,comm kill -9 $pid; sleep 1; ps -p $pid -o pid,comm - Practice
Kill everything a user is running
As root, start a
sleep 600as useralex(sudo -u alex sleep 600 &). List alex's processes, then terminate all of them with one command. Explain whypkill -fcan be dangerous.Check your result
pgrep -u alex -a sleepshows the process; aftersudo pkill -u alexit is gone.pkill -f PATTERNmatches the whole command line, so a pattern likesleepcan match more than you meant.Hint
pgrep -u USER,pkill -u USER. For a user's login sessions,loginctl terminate-user USERis the cleaner tool.Solution
sudo -u alex sleep 600 & pgrep -u alex -a sudo pkill -u alex sleep pgrep -u alex -a || echo none - Challenge
Read process states
List processes with their state letter. Find one of each state you can: running or runnable (
R), sleeping (S), and stopped (T, create one withsleep 600thenCtrl+Z). Explain what a zombie (Z) is and whykill -9on a zombie does nothing.Check your result
ps -eo pid,stat,commshowsSfor most,Rfor thepsitself,Tfor the stopped job. A zombie has already exited; only its parent can reap it by reading its exit status.Hint
ps -eo pid,ppid,stat,comm.Ctrl+Zstops the foreground job;bgresumes it in the background,fgreturns it.Solution
ps -eo pid,ppid,stat,comm | head sleep 600 # press Ctrl+Z jobs ps -o pid,stat,comm -p $(pgrep -n sleep) kill %1