Skip to lesson

Linux Command Line · Lesson 27 of 43

Processes: ps, top and kill

See what is running with ps, top and pgrep, understand process IDs and states, and stop programs safely with kill and signals.

Beginner to intermediate·13 min·Processes and the system·Updated 2026-11-24

UNDERSTAND IT. THEN TRY IT.

What you can do after this lesson

You will see what is running on a machine, find a program that misbehaves, and stop it in the safest way. These skills matter on servers, where you cannot just close a window to make something stop.

What a process is

A process is a running instance of a program. When you type ls, the shell starts a process, it runs, and it ends. Each process has:

  • a PID, its unique process ID;
  • a PPID, the PID of the parent that started it (your shell is the parent of the commands you run);
  • an owner, the user it runs as, which decides what it may touch;
  • a state, such as running, sleeping (waiting for something) or stopped.

The first process, PID 1, starts all the others. On most modern distributions it is systemd.

ps: a snapshot

ps with no options shows only processes from your current terminal. The shell stores its own PID in $$, so you can ask about it:

bash
ps -p $$ -o comm=
result
bash

For the whole system, two spellings are common and both work on Linux: ps aux (BSD style) and ps -ef (Unix style). The output looks like this, though the processes and numbers differ on every machine:

text
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1 169384 11236 ?        Ss   Nov23   0:03 /sbin/init
asha      2210  0.0  0.0  19640  5120 pts/0    Ss   09:14   0:00 -bash
asha      2548  0.0  0.0  21964  3488 pts/0    R+   09:20   0:00 ps aux

Read the columns: the owner, the PID, the share of CPU and memory in use, the terminal, the state, the start time and the command. A state of S is sleeping, R running, T stopped and Z a zombie, a finished process whose parent has not collected its result. Because the list is long, it is usually filtered with grep or pgrep:

text
ps aux | grep nginx

The grep command itself also appears in the list. A trick is to write grep [n]ginx, which does not match its own command line.

Custom columns and the process tree

-o chooses columns. ps -eo pid,ppid,user,comm shows the parent of each process, and ps -ef --forest or pstree draws the parent-and-child tree, which helps to see what started what. You can sort with --sort=-%mem to put the biggest memory users first.

top: the live view

top redraws the list every few seconds and shows load, memory and the busiest processes first. Press q to quit, M to sort by memory, P to sort by CPU, k to kill a process by PID, and 1 to show each CPU core. htop is a friendlier alternative on systems where it is installed. These tools need a real terminal, so we do not show their output here.

pgrep: find by name

pgrep prints the PIDs of processes whose names match, which is easier than reading ps output. We start a harmless background command, sleep, which just waits, and look for it:

bash
sleep 120 &
pgrep -f "sleep 120" | wc -l
kill $!
result
1

The trailing & starts a command in the background, which the next lesson explains. $! holds the PID of the last background process, and kill $! stopped it. The count is 1 because exactly one process matched. -f matches against the whole command line instead of only the program name.

Signals and kill

You stop or control a process by sending it a signal. Despite its name, kill simply sends any signal, and the default is a polite request to quit. The common ones:

Signal Number Meaning
SIGTERM 15 Please terminate. The program may clean up first (the default)
SIGINT 2 Interrupt, what Ctrl+C sends
SIGHUP 1 Hang up; many services reload their configuration on it
SIGKILL 9 Stop immediately; cannot be caught or ignored
SIGSTOP / SIGCONT 19 / 18 Pause and resume (numbers as on x86 and ARM Linux; some other architectures differ)

kill -l lists the signals, and kill -l with a number or name translates between them:

bash
kill -l 15
kill -l KILL
result
TERM
9

Always try the gentle signal first. SIGKILL gives the program no chance to save data, close files or remove temporary files. Here is the full sequence, with the exit status of the stopped process, which is 128 plus the signal number:

bash
sleep 300 &
pid=$!
kill $pid
wait $pid
echo "exit status: $?"
result
exit status: 143

15 plus 128 is 143, the usual status of a process ended by SIGTERM. Escalate only if the process ignores the polite request: kill -9 PID, or equivalently kill -KILL PID.

pkill and killall

pkill NAME and killall NAME send a signal to every process with that name. They are convenient but dangerous: pkill python ends every Python process you own. Check first with pgrep -a NAME, which prints the PIDs together with the full command lines.

Permissions and safety

You may signal only your own processes, while root may signal any. Never kill process 1, and think before stopping anything you do not recognise, because a system service may depend on it. On a server, restart services with their manager, as in sudo systemctl restart nginx, instead of killing them by hand.

Common mistakes

Jumping straight to kill -9. It skips cleanup. Try the default signal and wait a few seconds first.

Killing the wrong PID. Re-run ps or pgrep -a right before the kill, because PIDs get reused.

Using pkill with a loose name. It may match more than you expect.

Reading top as a list of problems. A process using lots of CPU for a short time is normal. Look for what stays high.

Exercise

Start sleep 500 &, find it with pgrep -a sleep, stop it with plain kill, and print the exit status with wait. Run ps -p $$ -o pid,ppid,comm and describe each field. On a real machine, open top, sort by memory with M, and write down the three biggest processes.

KNOWLEDGE CHECK

A quick check. You’ve got this.

Try each question. Every answer comes with an explanation.

0 / 4 understood
01 What does kill 1234 send by default?
02 Which command shows a live, updating list of processes?
03 What is a PID?
04 Why try kill before kill -9?

Ready for the next small win?

Mark the lesson complete when you are ready.

Course navigation

Tools and Workflow

Linux Command Line tutorial

Learn to work confidently in the terminal, from your first command to pipes, permissions, scripts and a real log-analysis project.

Your progress 0/43
Learning something new?Explore all courses