Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

LX
Linux
0/11 completed
Beginner
The Linux file systemUsers, groups and permissionsPackage managers
Intermediate
Processes and signalsDisks, storage and archivesNetworking commandsSSH and remote access
Advanced
systemd and servicesLogs and troubleshooting
Pro
Securing a serverPerformance and resource limits
Tracks / Linux / Intermediate

Processes and signals

Every program running on Linux is a process. Your shell is one, every command you start becomes one, and a server runs hundreds of them in the background. Knowing how to see, control and stop them is everyday server work: what's using all the CPU? why won't this app stop? what's listening on port 80?

Seeing processes

bash
ps
echo "---"
ps -ef | head -5
Output
    PID TTY          TIME CMD
    127 pts/0    00:00:00 bash
    128 pts/0    00:00:00 ps
---
UID          PID    PPID  C STIME TTY          TIME CMD
root           1       0  5 10:48 ?        00:00:00 /sbin/init
root          29       1  2 10:48 ?        00:00:00 /usr/lib/systemd/systemd-journald
root          89       1  0 10:48 ?        00:00:00 /usr/sbin/cron -f -P
message+      90       1  0 10:48 ?        00:00:00 @dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
Column Meaning
PID Process ID, unique while the process runs
PPID Parent's PID: the process that started this one
UID / USER Which user it runs as
TTY The terminal it's attached to (? means none: a background service)
CMD The command line

PID 1 is special: it's the first process the kernel starts (systemd on most systems), and it's the ancestor of all the others.

bash
sudo systemctl start nginx
ps -e --forest -o pid,ppid,user,cmd | grep -E "PID|init|journald|cron|nginx" | grep -v grep | head -7
Output
    PID    PPID USER     CMD
      1       0 root     /sbin/init
     29       1 root     /usr/lib/systemd/systemd-journald
     89       1 root     /usr/sbin/cron -f -P
    154       1 root     nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
    155     154 www-data  \_ nginx: worker process
    156     154 www-data  \_ nginx: worker process

--forest shows the family tree. You can see systemd starting the services, and nginx running one master process (as root) with worker processes (as www-data).

The useful ps recipes

bash
ps aux --sort=-%mem | head -4 | cut -c1-90
echo "---"
pgrep -a nginx | head -3
pgrep -c -u www-data
Output
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          29  1.9  0.1  41888 15488 ?        S<s  10:48   0:00 /usr/lib/systemd/system
root           1  5.3  0.1  20924 11500 ?        Ss   10:48   0:00 /sbin/init
message+      90  0.0  0.0   8396  3968 ?        Ss   10:48   0:00 @dbus-daemon --system -
---
154 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
155 nginx: worker process
156 nginx: worker process
10
Command Shows
ps aux Every process with CPU %, memory % and memory size (RSS, in KB)
ps aux --sort=-%cpu | head The top CPU users
ps -ef --forest Parent/child tree
pgrep -a name PIDs and command lines matching a name
pidof nginx Just the PIDs

Live view: top and htop

top updates every few seconds. Inside it, press P to sort by CPU, M by memory, k to kill, q to quit. htop (sudo apt install htop) is a friendlier, colourful version. In scripts, top -bn1 prints one snapshot:

bash
top -bn1 | head -5
Output
top - 10:48:21 up 9 days,  9:31,  0 user,  load average: 1.49, 0.79, 0.66
Tasks:  19 total,   1 running,  18 sleeping,   0 stopped,   0 zombie
%Cpu(s):  1.0 us,  0.0 sy,  0.0 ni, 99.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   7836.8 total,    320.7 free,   1634.8 used,   6108.4 buff/cache
MiB Swap:   1024.0 total,   1022.2 free,      1.8 used.   6201.9 avail Mem

The load average (three numbers: the last 1, 5 and 15 minutes) is roughly "how many processes wanted the CPU at once". Compare it with the number of CPU cores (nproc): a load of 4 on a 4-core machine means it's fully busy.

Foreground, background and jobs

bash
sleep 300 &
echo "started job with PID $!"
jobs
kill %1
wait %1
echo "job 1 ended with exit code $? (128 + 15: stopped by SIGTERM)"
Output
started job with PID 227
[1]+  Running                 sleep 300 &
job 1 ended with exit code 143 (128 + 15: stopped by SIGTERM)
Action How
Run in the background command &
List this shell's jobs jobs
Suspend the foreground job Ctrl + Z
Continue it in the background / foreground bg / fg
Stop the foreground job Ctrl + C

Background jobs still belong to your terminal: close it and they're usually killed. To keep something running after you log out, use nohup command &, a tmux session, or better, a systemd service (next-but-two lesson).

Signals

A signal is a message the kernel delivers to a process. kill sends them, despite its name:

Signal Number Meaning Sent by
SIGTERM 15 "Please exit cleanly" (the default) kill PID, systemctl stop
SIGINT 2 Interrupt Ctrl + C
SIGKILL 9 Die now. Can't be caught or ignored kill -9 PID
SIGHUP 1 Terminal closed; many daemons treat it as "reload your config" kill -HUP PID
SIGSTOP / SIGCONT 19 / 18 Pause / resume Ctrl + Z, fg
SIGUSR1 10 App-defined (e.g. "reopen log files") kill -USR1 PID

A program can catch most signals and react, for example finishing the current request before exiting. Here's a script that does:

worker.sh
#!/usr/bin/env bash
trap 'echo "got SIGTERM: finishing current job, then exiting"; exit 0' TERM
trap 'echo "got SIGHUP: reloading config"' HUP
echo "worker started"
while true; do sleep 0.2; done
bash
bash worker.sh &
pid=$!
sleep 0.5
kill -HUP "$pid"
sleep 0.5
kill "$pid"
wait "$pid"
echo "worker exited with code $?"
Output
worker started
got SIGHUP: reloading config
got SIGTERM: finishing current job, then exiting
worker exited with code 0

SIGKILL gives the process no chance to clean up: open files may be left half-written and temporary files stay behind. The polite order is always:

bash
bash worker.sh > /dev/null &
pid=$!
sleep 0.3
kill "$pid"                                         # 1. ask nicely (SIGTERM)
sleep 1
kill -0 "$pid" 2>/dev/null && kill -9 "$pid"        # 2. only if it's still alive
kill -0 "$pid" 2>/dev/null || echo "process $pid is gone"
Output
process 277 is gone

kill -0 PID sends no signal; it just checks whether the process exists. That's exactly what systemctl stop does for services: SIGTERM, wait (90 seconds by default), then SIGKILL.

Kill by name with pkill (careful: it matches any part of the command line unless you use -x):

bash
sleep 1001 & sleep 1002 &
pgrep -a sleep
pkill -f "sleep 100"
sleep 0.2
pgrep sleep > /dev/null || echo "no sleep processes left"
Output
298 sleep 1001
299 sleep 1002
no sleep processes left

Exit codes and zombies

When a process ends, its exit code waits for the parent to collect it. Until then it's a zombie (Z in ps): it uses no memory or CPU, only a slot in the process table. A few zombies are harmless; thousands mean a buggy parent program. Killing a zombie does nothing; it's already dead. Fix or restart the parent.

If a parent dies first, its children are adopted by PID 1, which collects them properly.

Priorities: nice and renice

Every process has a niceness from -20 (highest priority) to 19 (lowest). Being "nice" to others means a higher number:

bash
nice -n 10 sleep 60 &
ps -o pid,ni,cmd -p $!
renice -n 15 -p $! > /dev/null
ps -o ni= -p $!
sudo renice -n -5 -p $! > /dev/null
ps -o ni= -p $!
kill $!
Output
    PID  NI CMD
    320  10 sleep 60
 15
 -5

Run big batch jobs (backups, video encoding, compiling) with nice -n 19 so the web server stays responsive. Only root can give a process higher priority (a negative number). For disk-heavy jobs, ionice -c3 command does the same for disk access.

Inside /proc/PID

Everything ps shows comes from /proc. Each process has a folder:

bash
sleep 120 &
pid=$!
tr '\0' ' ' < /proc/$pid/cmdline; echo
grep -E "^(Name|State|PPid|VmRSS)" /proc/$pid/status
ls -l /proc/$pid/cwd | awk '{ print $NF }'
ls /proc/$pid/fd | wc -l
kill $pid
Output
sleep 120
Name:	sleep
State:	S (sleeping)
PPid:	343
VmRSS:	    1692 kB
/home/you
3

/proc/PID/fd lists the process's open files. lsof -p PID shows them with names, and lsof -i :80 shows which process has port 80 open. Both are helpful when a disk is full of a deleted-but-still-open log file, or a port is "already in use".

Practice

  1. Start sleep 500 in the background, find its PID three different ways ($!, pgrep, ps), then stop it with SIGTERM.
  2. Write a script that traps INT and prints "bye" when you press Ctrl + C.
  3. Show the five processes using the most memory.
  4. Run yes > /dev/null & (it uses 100% of a CPU), watch it in top, lower its priority with renice, then kill it.
✎ Check your understanding

1.Which signal does kill 1234 send?

2.Why should SIGKILL be a last resort?

3.What does nice -n 19 ./backup.sh do?

4.What's PID 1 on most Linux systems?

← Package managers