Linux Processes: ps, top and kill
Every running program is a process with its own number, called a PID. ps and top show what is running and how much it is using, and kill stops a process that is stuck.
Seeing what is running
ps auxLists every process, with its user, PID, CPU and memory use.
ps aux --sort=-%mem | headShows the processes using the most memory.
ps aux --sort=-%cpu | headShows the processes using the most CPU.
pgrep -a bashFinds processes by name and shows their PIDs.
Watching live with top
topShows running processes, updating every few seconds. If it's installed, htop is easier to read and use.
| Key | What it does |
|---|---|
P | Sort by CPU use |
M | Sort by memory use |
k | Kill a process (asks for the PID) |
1 | Show each CPU core |
q | Quit |
Stopping a process
kill 1234Asks process 1234 to close. Most programs tidy up and exit.
kill -9 1234Forces process 1234 to stop immediately. Use this only if a normal kill doesn't work, as unsaved work is lost.
pkill firefoxStops every process named firefox.
Be careful who you stop. Killing system processes can crash your computer. Check the name and PID with ps or pgrep first.
Quick reference
| Option | Meaning |
|---|---|
ps aux | List all processes |
pgrep -a name | Find a process by name |
top / htop | Watch processes live |
kill PID | Ask a process to close |
kill -9 PID | Force a process to stop |
pkill name | Stop processes by name |