← Operating Systems from Zero: Linux, Windows and macOS

Video coming soon

The written lesson below is ready now. The video is being published on our YouTube channel.

Follow ChangeCV on YouTube →

Lesson 5 of 6

Processes: ps aux, top, pgrep, kill and kill -9

See what is running on your computer and stop a program that is stuck, with a restaurant-kitchen analogy. Covers ps, top, pgrep, kill and kill -9, plus Get-Process and Stop-Process on Windows.

The idea in plain words

Think of a restaurant kitchen. Every order is a process, with a ticket number called the PID. A screen over the pass shows which cook is busiest, and that is top.

To cancel an order politely, you ask the cook to stop, and that is kill. If the cook ignores you, you pull the plug, and that is kill -9.

Commands in this lesson

ps aux
List every process, one per line, with its owner, PID, processor and memory use. On Windows use Get-Process.
top
Show a live view of the busiest processes. Press q to quit. On macOS use top -o cpu, and on Windows open Task Manager.
pgrep -l node
Find the PID of a process by name. In PowerShell use Get-Process node.
kill 2210
Ask a process to stop, politely, using its PID. In PowerShell use Stop-Process.
kill -9 2210
End a stuck process at once, with no time to save. Last resort. On Windows use Stop-Process -Force or taskkill /F.

Written notes from the video

What is running on my computer?

Your computer is running hundreds of programs right now. This is Operating Systems from zero, episode five: processes. Five commands to see what is running, and to stop it.

The restaurant kitchen analogy

Think of a restaurant kitchen. Every order is a process, with a ticket number. That number is the PID. A screen over the pass shows which cook is busiest. That is top. To cancel an order politely, you ask the cook to stop. That is kill. If the cook ignores you, you pull the plug. That is kill -9.

Command 1: ps aux

Command one: ps aux. It lists every process, one per line. Each line shows the owner, the PID, how much processor and memory it uses, and the command. Here, head shows only the first three lines. On macOS, the same command works. On Windows PowerShell, use Get-Process.

Command 2: top

Command two: top. It shows a live view of the busiest processes. The header shows how long the computer has been up, and how much memory is free. Press q to quit. On macOS, use top -o cpu. On Windows, open Task Manager.

Command 3: pgrep

Command three: pgrep. It finds the PID of a process by name. pgrep -l node shows 2210 node. We have our number. In the networking course, the command lsof gave you a PID as well. On Windows PowerShell, use Get-Process node.

Command 4: kill

Command four: kill. It asks a process to stop, politely, using its PID. The process gets time to save its work and close. Now pgrep -l node prints nothing. The process is gone. On Windows PowerShell, the command is Stop-Process.

Command 5: kill -9

Command five: kill -9. Some processes get stuck, and ignore the polite request. The -9 flag cannot be ignored. The system ends the process at once. It gets no time to save anything, so use it only as a last step. On Windows, use Stop-Process with Force, or taskkill.

On Windows and macOS

On Windows PowerShell, Get-Process lists the processes. Sort them by processor time, and take the first three. Stop-Process with an I D stops one. taskkill with slash F forces it. On macOS, the header of top looks different, but kill works the same.

Recap

Recap. ps aux lists processes. top shows the busiest. pgrep finds a P I D by name. kill asks a process to stop, and kill -9 forces it.

Next episode

Next episode: package managers. You will learn to install software from the terminal. Subscribe so you do not miss it, and find written notes for every lesson at changecv.com/skills.

Common questions

What is a PID?

A PID is a process ID, the number the system gives each running program. You need it to stop a process with kill.

What is the difference between kill and kill -9?

kill asks the process to stop and gives it time to save its work and close. kill -9 cannot be ignored and ends the process at once, so unsaved work is lost. Try plain kill first.

How do I find the PID of a program?

Use pgrep with part of the name, or look at the PID column in ps aux or top. lsof -i with a port number also shows the PID of the program using that port.

What is the Windows equivalent of top?

Task Manager is the closest. In PowerShell, Get-Process lists processes, and you can sort the output by CPU time with Sort-Object.

Why can I not stop some processes?

You can only stop your own processes. A process owned by another user or by root needs sudo, and stopping system processes can break your computer, so check the name first.