top: a live view
ps takes a snapshot. top keeps the list on the screen and refreshes it every few seconds, busiest processes first. It takes over the whole terminal; press q to quit.
top| Keys | What it does |
|---|---|
| q | Quit |
| P | Sort by CPU use (the default) |
| M | Sort by memory use |
| u | Show only one user's processes (type the name, then Enter) |
| k | Kill a process (type its PID, then Enter) |
| h | Help: every key |
top -b -n 1 prints one screenful as normal text instead, so you can save or pipe it:
top -b -n 1 | head -n 12In the terminal
student@vulntech:~$ top -b -n 1 | head -n 12 top - 14:57:46 up 20 min, 0 user, load average: 0.29, 0.17, 0.56 Tasks: 55 total, 1 running, 54 sleeping, 0 stopped, 0 zombie %Cpu(s): 32.1 us, 40.4 sy, 0.0 ni, 1.8 id, 0.0 wa, 22.9 hi, 2.8 si, 0.0 st MiB Mem : 217.0 total, 179.6 free, 20.9 used, 23.7 buff/cache MiB Swap: 0.0 total, 0.0 free, 0.0 used. 196.2 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1601 student 20 0 3204 2496 1068 R 36.6 1.1 0:02.44 top 14 root 20 0 0 0 0 I 1.2 0.0 0:02.69 kworker/u+ 2 root 20 0 0 0 0 S 0.0 0.0 0:00.05 kthreadd 4 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 kworker/R+ 1 root 20 0 1636 864 824 S 0.0 0.4 0:03.32 init
The first lines are a summary: how long the machine has been up, the number of tasks, how busy the CPU is (us = programs, sy = the kernel, id = idle) and the memory.
uptime and the load average
uptime
nprocIn the terminal
student@vulntech:~$ uptime 14:54:43 up 19 min, 0 user, load average: 0.31, 0.20, 0.60 student@vulntech:~$ nproc 1
The three load average numbers are how many processes were running or waiting to run, averaged over the last 1, 5 and 15 minutes. Compare them with the number of CPUs (nproc): this machine has one, so a load of 1.00 would mean it's fully busy, and more than that means programs are queuing.
free: memory
free -hIn the terminal
student@vulntech:~$ free -h total used free shared buff/cache available Mem: 217Mi 16Mi 183Mi 28Ki 23Mi 200Mi Swap: 0B 0B 0B
-h prints human-readable sizes. Linux keeps spare memory busy as a cache of files it has read (buff/cache) and gives it back when programs need it, so look at available to see how much memory is really left.
Being nice: nice and renice
When several processes want the CPU, Linux shares it according to their niceness, from -20 (greedy, goes first) to 19 (very nice, lets others go first). Programs start at 0. nice -n 10 starts a program with a niceness of 10, and the NI column shows it. (The & at the end runs it in the background; the next lessons explain how.)
nice -n 10 sleep 300 &
ps -o pid,ni,commIn the terminal
student@vulntech:~$ nice -n 10 sleep 300 & [1] 1601 student@vulntech:~$ ps -o pid,ni,comm PID NI COMMAND 1294 0 bash 1601 10 sleep 1610 0 ps
renice changes a running process. $! is the PID of the last program you started in the background. You can always be nicer, but only root may make a process greedier:
sleep 300 &
renice -n 15 -p $!
renice -n 5 -p $!
sudo renice -n 5 -p $!In the terminal
student@vulntech:~$ sleep 300 & [1] 1601 student@vulntech:~$ renice -n 15 -p $! 1601 (process ID) old priority 0, new priority 15 student@vulntech:~$ renice -n 5 -p $! renice: failed to set priority for 1601 (process ID): Permission denied student@vulntech:~$ sudo renice -n 5 -p $! [sudo] password for student: 1601 (process ID) old priority 15, new priority 5
Tip: Run long, heavy jobs (backups, big compressions) with
nice -n 19so the machine stays responsive for everyone else.
Remember:
topwatches live (q quits).uptimeshows the load,free -hthe memory. Higher niceness = lower priority.