What a process is: ps, top, PID and PPID
This lesson is open to everyone. The other lessons open with a package: sign in or sign up, then choose a package.
Key points
- Every process has a unique number, the PID, and the number of the process that started it, the PPID; the first process is
systemd(PID 1). ps auxshows all processes,ps -ef --forestthe parent–child tree.top(orhtop) is the live view:Psorts by CPU,Mby memory,qquits.- States:
Rrunning,Ssleeping,Dwaiting for disk,Zzombie,Tstopped. pgrep -a nginxandpidof nginxfind a process by name.
Program and process
A program is a file sitting on disk (/usr/sbin/nginx). When you start it, a process appears in memory: a live object with its own number, user, open files and memory. One program can have several processes — nginx works with one master process and several worker processes.
PID and PPID
ali@srv-web01:~$ echo $$ 2741 ali@srv-web01:~$ ps -o pid,ppid,user,cmd -p $$ PID PPID USER CMD 2741 2740 ali -bash
$$ is the PID of the current shell. Its parent (PPID 2740) is the SSH session. Every process is created by another process, and at the head of that chain stands systemd (PID 1).
ali@srv-web01:~$ ps -ef --forest | grep -E "sshd|bash|nginx" | grep -v grep root 1032 1 0 08:55 ? 00:00:00 sshd: /usr/sbin/sshd -D [listener] root 2702 1032 0 09:14 ? 00:00:00 \_ sshd: ali [priv] ali 2740 2702 0 09:14 ? 00:00:00 \_ sshd: ali@pts/0 ali 2741 2740 0 09:14 pts/0 00:00:00 \_ -bash root 1210 1 0 08:55 ? 00:00:00 nginx: master process /usr/sbin/nginx www-data 1211 1210 0 08:55 ? 00:00:02 \_ nginx: worker process www-data 1212 1210 0 08:55 ? 00:00:02 \_ nginx: worker process
Notice: the nginx master runs as root (to bind to port 80), while the workers run as www-data — the security principle from the previous unit.
ps aux: look at everything
ali@srv-web01:~$ ps aux --sort=-%mem | head -n 4 USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND mysql 1340 1.2 18.4 1789420 372400 ? Ssl 08:55 1:12 /usr/sbin/mysqld root 842 0.1 2.3 289340 47212 ? Ssl 08:55 0:04 /usr/lib/snapd/snapd www-data 1211 0.3 0.6 56108 12880 ? S 08:55 0:02 nginx: worker process
| Column | Meaning |
|---|---|
%CPU, %MEM | Share of processor and memory |
VSZ | Virtual memory (KB) — reserved, not all of it used |
RSS | Memory actually held in RAM (KB) — look at this |
STAT | State |
TIME | CPU time used so far |
--sort=-%mem sorts by memory, descending; for CPU use --sort=-%cpu.
Process states
| STAT | Meaning |
|---|---|
R | Running or waiting in the queue |
S | Sleeping, waiting for an event (the most common) |
D | Waiting for disk/network, cannot be interrupted |
T | Stopped (Ctrl+Z) |
Z | Zombie: finished, but the parent has not "collected" it |
Extra letters: s session leader, l multi-threaded, + in the foreground, < high priority. Many processes in state D are a sign of a disk problem.
top: the live view
ali@srv-web01:~$ top top - 10:31:07 up 1:36, 1 user, load average: 0.42, 0.35, 0.30 Tasks: 128 total, 1 running, 127 sleeping, 0 stopped, 0 zombie %Cpu(s): 3.1 us, 1.0 sy, 0.0 ni, 95.6 id, 0.3 wa, 0.0 hi, 0.0 si, 0.0 st MiB Mem : 1967.4 total, 312.6 free, 842.1 used, 812.7 buff/cache MiB Swap: 2048.0 total, 2048.0 free, 0.0 used. 956.3 avail Mem
| Key | What it does |
|---|---|
P | Sort by CPU |
M | Sort by memory |
k | Send a signal to a process (next lesson) |
1 | Show each core separately |
q | Quit |
The two most important numbers in the header: wa (percentage waiting for disk — if high, the disk is the bottleneck) and avail Mem (memory really available for new processes). htop does the same with colours and mouse support: sudo apt install htop.
Finding a process by name
ali@srv-web01:~$ pgrep -a nginx 1210 nginx: master process /usr/sbin/nginx 1211 nginx: worker process 1212 nginx: worker process ali@srv-web01:~$ pidof mysqld 1340 ali@srv-web01:~$ pgrep -u www-data -c 2
ps aux | grep nginx works too, but grep itself shows up in the result; pgrep is cleaner.
In the Linux simulator, see what is running on the server with ps aux, pgrep -a and top.
Check yourself
5 questions · answers are shown instantly · 3 correct answers complete the lesson
1Which process is PID 1 in Linux?
2What is the PPID?
3Which column shows the RAM a process actually uses?
4Which key sorts top by memory?
P is CPU, M is memory.5What does state Z mean?
Comments
Sign in to comment.