terminal.cab

Linux Basics Lesson 21 of 38

guestLevel 1Badges and progress0/100 XP, XP to the next levelStreak0

Finding and ending processes

10 minutes

Learning goals

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:

  • aux all processes of all users, with CPU and memory
  • -ef all processes, with the parent PID
  • --sort=-%cpu biggest CPU user first

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 --sort

in the glossary: ps

ps 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:

  • -b -n 1 one screen as plain text, without the full-screen view

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

in the glossary: 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:

  • -9 KILL, no chance to clean up, the last resort

Example:

kill 3021
pgrep -a python3
2114 python3 app.py
4711 python3 export.py

in the glossary: kill

The 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.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

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.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

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.py

Before the command, in your home folder ~

  • ~/.bashrc
  • ~/.profile
  • ~/.secret
  • ~/Documents/
  • ~/logs/
  • ~/notes.txt
  • ~/projects/
  • ~/archive/
What does pgrep -f report.py print?

Check works once you have chosen an answer.

Put in order

A process is hogging the CPU. Put the steps in order.

Steps

  1. Wait a few seconds and look again.
  2. Find the PID with ps or pgrep.
  3. Send TERM with kill.
  4. Find out why it ran away.
  5. Only if it is still there, use kill -9.

Next up: your own programs in the background.

Quiz

  1. Question 1 of 3ps aux shows python3 expor. Why not the full name?

    Check works once you have chosen an answer.

  2. Question 2 of 3What is the difference between killall and pkill?

    Check works once you have chosen an answer.

  3. Question 3 of 3Load average 4.07 on a machine with 4 cores. How do you read it?

    Check works once you have chosen an answer.

Cards

  1. Card 1 of 3

    Tap the card or press Space to turn it over.

  2. Card 2 of 3

    Tap the card or press Space to turn it over.

  3. 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

Next: lesson 22, Foreground, background, staying on (9 min)

What you can do now: ps aux --sort=-%cpu, top -b -n 1, kill 3021

+0 XP in this lesson

  1. Lesson 1: open
  2. Lesson 2: open
  3. Lesson 3: open
  4. Lesson 4: open
  5. Lesson 5: open
  6. Lesson 6: open
  7. Lesson 7: open
  8. Lesson 8: open
  9. Lesson 9: open
  10. Lesson 10: open
  11. Lesson 11: open
  12. Lesson 12: open
  13. Lesson 13: open
  14. Lesson 14: open
  15. Lesson 15: open
  16. Lesson 16: open
  17. Lesson 17: open
  18. Lesson 18: open
  19. Lesson 19: open
  20. Lesson 20: open
  21. Lesson 21: done
  22. Lesson 22: open
  23. Lesson 23: open
  24. Lesson 24: open
  25. Lesson 25: open
  26. Lesson 26: open
  27. Lesson 27: open
  28. Lesson 28: open
  29. Lesson 29: open
  30. Lesson 30: open
  31. Lesson 31: open
  32. Lesson 32: open
  33. Lesson 33: open
  34. Lesson 34: open
  35. Lesson 35: open
  36. Lesson 36: open
  37. Lesson 37: open
  38. Lesson 38: open
1 of 38

Next: lesson 22, Foreground, background, staying on (9 min) That's enough for today

Path complete

You finished all 38 lessons of Linux Basics.

Back to the path

Cheat sheet

The most important command lines of this module on one A4 page, black and white, to print or keep on your phone.