ps, kill & top
Every running program is a process with a unique number, the PID (process ID). ps lists processes, kill sends them signals (for example "please stop") and top shows a live, updating view of what is using your CPU and memory.
ps: a snapshot of processes
Plain ps shows only the processes of your current terminal. The two common "show everything" forms are BSD-style ps aux and System V-style ps -ef. Typical output on a server (yours will differ):
ps aux | head -n 5USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 167744 11800 ? Ss 08:01 0:02 /sbin/init
root 612 0.0 0.0 15420 7168 ? Ss 08:01 0:00 sshd: /usr/sbin/sshd -D
www-data 1200 0.3 1.2 412312 98764 ? S 08:02 0:41 nginx: worker process
learner 2311 0.0 0.0 9704 4352 pts/0 Ss 09:15 0:00 -bash| Column | Meaning |
|---|---|
PID |
Process ID |
%CPU, %MEM |
Share of CPU and memory |
RSS |
Memory actually in use (KiB) |
TTY |
Terminal (? = none, e.g. a background service) |
STAT |
State: R running, S sleeping, D waiting for disk, Z zombie, T stopped |
COMMAND |
The command line |
Because PIDs and times change every run, the runnable examples choose their own columns with -o and leave the PID out:
sleep 300 &
sleep 400 &
ps -o user=,stat=,args= -C sleep
echo "sleep processes: $(pgrep -c sleep)"
kill %1 %2learner S sleep 300
learner S sleep 400
sleep processes: 2
-C NAME selects processes by command name, and the = after each column name hides the header. pgrep finds processes by name (-c counts them, -a shows the command line, -u USER filters by user).
Signals and kill
kill doesn't necessarily kill: it sends a signal, and the process decides how to react (except to KILL and STOP, which can't be caught).
| Signal | Number | Meaning | Typical sender |
|---|---|---|---|
SIGTERM |
15 | Please terminate (clean up first) | kill PID (the default) |
SIGINT |
2 | Interrupt | Ctrl+C |
SIGHUP |
1 | Terminal closed / reload config | many daemons reload on it |
SIGKILL |
9 | Stop immediately, no cleanup | kill -9 PID, last resort |
SIGSTOP / SIGCONT |
19 / 18 | Pause / resume | Ctrl+Z, fg |
sleep 600 &
job=$!
kill -0 "$job" && echo "process is alive"
kill -TERM "$job"
wait "$job"
echo "exit status: $? (128 + 15 = killed by SIGTERM)"
kill -0 "$job" 2>/dev/null || echo "process is gone"
kill -l 9 15process is alive
exit status: 143 (128 + 15 = killed by SIGTERM)
process is gone
KILL
TERM
$!holds the PID of the last background command (stored in a variable but never printed, since it changes every run).kill -0 PIDsends no signal; it only checks the process exists.- A process killed by signal N reports exit status 128 + N.
Tip: Always try
kill PID(TERM) first and give the program a few seconds to save its work.kill -9skips all cleanup and can leave half-written files or stale lock files behind.
Killing by name
pkill PATTERN and killall NAME signal every matching process. Powerful, so check the matches first with pgrep -a:
sleep 1001 &
sleep 1002 &
sleep 2001 &
pgrep -a sleep | cut -d' ' -f2-
pkill -f 'sleep 100'
wait %1 %2
pgrep -a sleep | cut -d' ' -f2-
pkill sleepsleep 1001
sleep 1002
sleep 2001
sleep 2001
-f matches against the full command line, not just the program name, so 'sleep 100' caught the first two sleeps and left sleep 2001 running. (cut removes the PID column.)
top and htop
top shows a continuously refreshing table of the busiest processes plus a summary of load, CPU and memory. It's interactive, so here is what it looks like:
top - 10:42:01 up 3 days, 2:11, 1 user, load average: 0.52, 0.48, 0.40
Tasks: 143 total, 1 running, 142 sleeping, 0 stopped, 0 zombie
%Cpu(s): 4.1 us, 1.2 sy, 0.0 ni, 94.5 id, 0.1 wa, 0.0 hi, 0.1 si
MiB Mem : 7951.2 total, 2310.4 free, 3120.9 used, 2519.9 buff/cache
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1200 www-data 20 0 412312 98764 10240 S 3.0 1.2 0:41.20 nginx
2890 postgres 20 0 220144 40120 36400 S 1.0 0.5 0:12.03 postgres| Key in top | Action |
|---|---|
q |
Quit |
P / M |
Sort by CPU / by memory |
k |
Kill a process (asks for the PID) |
1 |
Show each CPU core |
u |
Show one user's processes |
htop (install it with your package manager) is a friendlier, colourful version with mouse support. For a single snapshot in a script, use top -b -n 1 (batch mode).
Limiting run time with timeout
A related everyday tool: timeout DURATION CMD kills a command that runs too long, and exits with status 124 when it had to:
timeout 1 sleep 10
echo "status: $?"
timeout 5 echo "fast command"
echo "status: $?"status: 124
fast command
status: 0
Test yourself
Which signal does kill PID send by default?
- SIGKILL (9)
- SIGTERM (15)
- SIGINT (2)
- SIGHUP (1)
A process was stopped by SIGKILL. What exit status does wait report?
- 9
- 1
- 137
- 255
Which command checks whether a process is still running without affecting it?
- kill -9 PID
- kill -0 PID
- kill -STOP PID
- top -k PID