Linux Basics Lesson 21 of 38
Finding and ending processes
10 minutes
Learning goals
- List processes with ps aux and ps -ef, and find one by name with pgrep -a.
- Read load, CPU and memory in top.
- End a process with kill, and only then with kill -9.
- Check with pgrep before you use killall or pkill.
ps: who is running?
Each process has a PID, a parent (PPID) and a user. ps aux lists everything with CPU and memory, ps -ef adds the parent. With --sort=-%cpu the greediest comes first:
ps aux --sort=-%cpu
Purpose: Lists the processes, with their PID, user, CPU and memory.
Syntax: ps aux [--sort=-%cpu] | ps -ef
Options:
Example:
ps aux --sort=-%cpu
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
lerner 4711 397 39.0 3600000 3145728 ? S 08:41 80:00 python3 expor
app 2114 1.2 1.2 412000 98304 ? Ss 06:01 0:00 python3 app.p
lerner 3021 0.8 0.5 180000 40960 pts/0 S 08:10 0:00 python3 repor
root 901 0.1 0.0 55000 7424 ? Ss 06:00 0:00 nginx: master
www-data 902 0.1 0.0 55000 5120 ? S 06:00 0:00 nginx: worker
root 1 0.0 0.1 22416 13056 ? Ss Oct05 0:02 /sbin/init
lerner 4242 0.0 0.0 8608 5120 pts/0 Ss 09:01 0:00 bash
lerner 4712 0.0 0.0 6788 4136 pts/0 R+ 09:02 0:00 ps aux --sortps cuts its lines at the width of the terminal, here 80 characters, which is why you read python3 expor and no more. It does not tell you which Python script that is. pgrep -a gives you the full command line with the PID:
pgrep -a python3
2114 python3 app.py
3021 python3 report.py
4711 python3 export.py
On a real server, ps auxww shows the lines in full. The simulator does not know that form and answers with a hint. grep is not loaded on this page, so ps aux | grep python ends in command not found here, but it is an everyday move on real machines.
top: the dashboard
top is the live view: load, CPU, memory and the process list. q closes it. Inside, P sorts by CPU and M by memory. As a one-off printout, top -b -n 1 works in a pipe or a log:
top -b -n 1
Purpose: Shows load, CPU, memory and the busiest processes at a glance.
Syntax: top | top -b -n 1
Options:
Example:
top -b -n 1
top - 09:02:00 up 12 days, 3:02, 1 user, load average: 3.92, 2.84, 1.70
Tasks: 8 total, 1 running, 7 sleeping, 0 stopped, 0 zombie
%Cpu(s): 91.2 us, 6.1 sy, 0.0 ni, 2.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 7860.0 total, 5092.0 free, 1472.0 used, 1680.0 buff/cache
MiB Swap: 0.0 total, 0.0 free, 0.0 used. 6388.0 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
4711 lerner 20 0 3600000 3.0g 786432 S 397.0 39.1 80:00.00 python3
2114 app 20 0 412000 98304 24576 S 1.2 1.2 0:00.00 python3
3021 lerner 20 0 180000 40960 10240 S 0.8 0.5 0:00.00 python3
901 root 20 0 55000 7424 1856 S 0.1 0.1 0:00.00 nginx
902 www-data 20 0 55000 5120 1280 S 0.1 0.1 0:00.00 nginx
1 root 20 0 22416 13056 3264 S 0.0 0.2 0:02.00 systemd
4242 lerner 20 0 8608 5120 1280 S 0.0 0.1 0:00.00 bash
4712 lerner 20 0 6788 4136 1034 R 0.0 0.1 0:00.00 top
The load average is three numbers, for 1, 5 and 15 minutes. It counts the processes that want to run. Permanently above the number of cores means they are queuing. The column COMMAND shows only the program name, python3, so the full line comes from pgrep -a again. htop is a nicer alternative you can install on real servers. It is not installed here, so htop answers command not found, as on a fresh Debian.
On a real server, top -b -n 1 | head -n 12 keeps only the top of a long list.
kill: first ask, then insist
kill sends a signal. Without a name that is TERM, a request to finish. -9 is KILL, the end with no warning.
kill 3021
Purpose: Sends a signal to a process, TERM if you name none.
Syntax: kill [-SIGNAL] PID... | killall NAME | pkill PATTERN
Options:
Example:
kill 3021
pgrep -a python3
2114 python3 app.py
4711 python3 export.pyThe other Python processes are still there, because you named one PID. kill prints nothing when it works, so look afterwards.
Practice
Terminal task 1/2
The CPU is at its limit. Show the processes so that you can see which one uses the most. Its PID is what you are after.
Task 1/2: The CPU is at its limit. Show the processes so that you can see which one uses the most. Its PID is what you are after.
The CPU is at its limit. Show the processes so that you can see which one uses the most. Its PID is what you are after.
Task complete
Solution
ps aux --sort=-%cpu Why: A %CPU of 397 means nearly four cores, which is possible on a machine with four. The first line tells you who to look at.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Terminal task 2/2
python3 report.py has PID 3021 and is no longer needed. End it politely. The other processes keep running.
Task 2/2: python3 report.py has PID 3021 and is no longer needed. End it politely. The other processes keep running.
python3 report.py has PID 3021 and is no longer needed. End it politely. The other processes keep running.
Task complete
Solution
kill 3021 Why: TERM gives the process the chance to clean up. And you only hit this one PID, not everything that is also called python3.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Fill the gap
kill without a name sends the signal: SIG
kill without a name sends the signal: SIG…
Check works once you have typed an answer.
Predict the output
Command
pgrep -f report.pyBefore the command, in your home folder ~
- ~/.bashrc
- ~/.profile
- ~/.secret
- ~/Documents/
- ~/logs/
- ~/notes.txt
- ~/projects/
- ~/archive/
Check works once you have chosen an answer.
Put in order
A process is hogging the CPU. Put the steps in order.
Steps
- Wait a few seconds and look again.
- Find the PID with ps or pgrep.
- Send TERM with kill.
- Find out why it ran away.
- Only if it is still there, use kill -9.
Next up: your own programs in the background.
Quiz
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
Cards
-
Card 1 of 3
Tap the card or press Space to turn it over.
-
Card 2 of 3
Tap the card or press Space to turn it over.
-
Card 3 of 3
Tap the card or press Space to turn it over.
End of the lesson
Still open: 5 exercises, 3 quiz questions
What you can do now: ps aux --sort=-%cpu, top -b -n 1, kill 3021
+0 XP in this lesson
- Lesson 1: open
- Lesson 2: open
- Lesson 3: open
- Lesson 4: open
- Lesson 5: open
- Lesson 6: open
- Lesson 7: open
- Lesson 8: open
- Lesson 9: open
- Lesson 10: open
- Lesson 11: open
- Lesson 12: open
- Lesson 13: open
- Lesson 14: open
- Lesson 15: open
- Lesson 16: open
- Lesson 17: open
- Lesson 18: open
- Lesson 19: open
- Lesson 20: open
- Lesson 21: done
- Lesson 22: open
- Lesson 23: open
- Lesson 24: open
- Lesson 25: open
- Lesson 26: open
- Lesson 27: open
- Lesson 28: open
- Lesson 29: open
- Lesson 30: open
- Lesson 31: open
- Lesson 32: open
- Lesson 33: open
- Lesson 34: open
- Lesson 35: open
- Lesson 36: open
- Lesson 37: open
- Lesson 38: open
Cheat sheet
-
ps aux --sort=-%cpuall processes, biggest first -
pgrep -a <name>PID and full command line -
kill <pid>ask it to end (TERM) -
kill -9 <pid>last resort (KILL) -
top -b -n 1load, CPU and memory as text
The most important command lines of this module on one A4 page, black and white, to print or keep on your phone.
- Download PDF (PDF, 19 KB)
- View as image (PNG, 57 KB)