$ run as a normal user# run as root (or with sudo)
Skills in this chapter
- List processes, control jobs and send signals
Know how processes start
At boot the kernel starts the first process (PID 1, normally systemd). Every other process is started by a parent process.
- Each process has a number: the PID (process ID).
- A process belongs to a user and has a state, memory, open files and a priority.
- Programs that run in the background without a terminal are called daemons (for example
sshd).
List processes with ps
ps shows processes. Without options it shows only the processes of your terminal.
ps
PID TTY TIME CMD
5198 pts/1 00:00:00 bash
10129 pts/1 00:00:00 ps
ps x
ps aux | less
ps aux --sort=-%mem | head -5
ps -ef| Option | Shows |
|---|---|
ps x | All your processes, also those without a terminal |
ps aux | All processes of all users, with owner, CPU and memory |
ps -ef | All processes, System V style, with the parent PID |
| STAT | Meaning |
|---|---|
R | Running or ready to run |
S | Sleeping (waiting for something) |
D | Waiting for disk or other I/O, cannot be stopped |
T | Stopped |
Z | Zombie: finished, but the parent has not cleaned it up |
< / N | High priority / low priority ("nice") |
Columns of ps aux: USER, %CPU, %MEM, VSZ (virtual memory), RSS (real memory in KB), START, TIME, COMMAND.
Watch processes live with top
top shows the busiest processes and refreshes every 3 seconds. The header shows the load and memory.
top
top - 14:59:20 up 6:30, 2 users, load average: 0.07, 0.02, 0.00
Tasks: 109 total, 1 running, 106 sleeping, 0 stopped, 2 zombie
%Cpu(s): 0.7 us, 1.0 sy, 0.0 ni, 98.3 id, 0.0 wa- load average: how many processes wait for the CPU, over 1, 5 and 15 minutes. Below the number of CPU cores is fine.
us= user programs,sy= kernel,id= idle,wa= waiting for disk.- Keys in top:
hhelp,Msort by memory,Psort by CPU,kkill,qquit.
Stop, background and foreground
Ctrl-c interrupts a program. & starts it in the background. jobs, fg and bg move jobs around.
xlogo
^C
xlogo &
[1] 28236
jobs
[1]+ Running xlogo &
fg %1
xlogo
^Z
[1]+ Stopped xlogo
bg %1
[1]+ xlogo &| Action | How |
|---|---|
| Interrupt (stop for good) | Ctrl-c |
| Start in the background | command & |
| List the jobs of this shell | jobs |
| Bring to the foreground | fg %1 |
| Pause the foreground job | Ctrl-z |
| Continue a paused job in the background | bg %1 |
At work
You started a long command and forgot the &? Press Ctrl-z, then type bg.
Change the priority with nice and renice
The niceness goes from -20 (most CPU) to 19 (least CPU). Normal is 0. A "nice" process lets others go first.
Send signals with kill
kill sends a signal to a process. The name is a bit wrong: it can do more than stop processes.
| Number | Name | Meaning |
|---|---|---|
| 1 | HUP | Hang up; many daemons read their settings again |
| 2 | INT | Interrupt, same as Ctrl-c |
| 9 | KILL | Stop at once; the program cannot catch it or clean up |
| 15 | TERM | Please end (the default of kill) |
| 18 | CONT | Continue after a stop |
| 19 | STOP | Pause; cannot be ignored |
| 20 | TSTP | Pause from the terminal, same as Ctrl-z |
Watch out
Use kill -9 only when kill (TERM) does not work. With KILL the program cannot save its data or remove its lock files.
Note
A program started from a terminal gets HUP when the terminal closes. nohup command & keeps it running; on systemd systems a service unit is the better way.
Signal many processes with killall
killall name sends a signal to every process with that name. pkill matches parts of names.
Shut down the system
Only an admin can shut down. shutdown can wait and warn the logged-in users.
Use more process tools
A few more commands help you see what the system does.
Summary of the chapter
ps and top show processes; &, jobs, fg, bg control jobs; kill sends signals.