Advanced
Searching inSummaries
How to search
  • Words find lesson titles, text and code. Filter by type (guides, courses, programs, books) or stay in this book.
  • book:rhcsa and ch:8 narrow the search; #code finds a word only in commands of the lessons.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

Chapter 10Name in the book: Processes

See and control processes

Linux runs many programs at the same time. Each running program is a process. The kernel shares the CPU between them very fast, so they all seem to run at once. This chapter shows how to list processes, move them to the background, stop them with signals, and shut the system down.

  • 10 Lessons
  • 18 Commands
  • 1 Labs

$ run as a normal user# run as root (or with sudo)

Skills in this chapter

  • List processes, control jobs and send signals

10.1

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).

Commands in this lesson

10.2

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
OptionShows
ps xAll your processes, also those without a terminal
ps auxAll processes of all users, with owner, CPU and memory
ps -efAll processes, System V style, with the parent PID
STATMeaning
RRunning or ready to run
SSleeping (waiting for something)
DWaiting for disk or other I/O, cannot be stopped
TStopped
ZZombie: finished, but the parent has not cleaned it up
< / NHigh priority / low priority ("nice")

Columns of ps aux: USER, %CPU, %MEM, VSZ (virtual memory), RSS (real memory in KB), START, TIME, COMMAND.

10.3

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: h help, M sort by memory, P sort by CPU, k kill, q quit.

Commands in this lesson

10.4

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 &
ActionHow
Interrupt (stop for good)Ctrl-c
Start in the backgroundcommand &
List the jobs of this shelljobs
Bring to the foregroundfg %1
Pause the foreground jobCtrl-z
Continue a paused job in the backgroundbg %1

At work

You started a long command and forgot the &? Press Ctrl-z, then type bg.

Commands in this lesson

10.5

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.

nice -n 10 tar czf backup.tgz /home
ps -l
renice -n 5 -p 28236
sudo renice -n -5 -p 28236
  • Normal users can only make their processes nicer (higher number). Only root can give a negative number.

10.6

Send signals with kill

kill sends a signal to a process. The name is a bit wrong: it can do more than stop processes.

NumberNameMeaning
1HUPHang up; many daemons read their settings again
2INTInterrupt, same as Ctrl-c
9KILLStop at once; the program cannot catch it or clean up
15TERMPlease end (the default of kill)
18CONTContinue after a stop
19STOPPause; cannot be ignored
20TSTPPause from the terminal, same as Ctrl-z
kill 28401
kill -1 28401
kill -INT 28401
kill -SIGTERM 28401
kill %1
kill -l

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.

10.7

Signal many processes with killall

killall name sends a signal to every process with that name. pkill matches parts of names.

killall xlogo
killall -u bob firefox
pkill -HUP rsyslogd
pgrep -l ssh
  • You can only signal your own processes. Root can signal all.

10.8

Shut down the system

Only an admin can shut down. shutdown can wait and warn the logged-in users.

sudo shutdown -h now
sudo shutdown -r +10 "Reboot for updates"
sudo reboot
sudo poweroff

10.9

Use more process tools

A few more commands help you see what the system does.

CommandWhat it shows
pstreeProcesses as a tree (parent → child)
vmstat 5Memory, swap, disk and CPU every 5 seconds
free -hMemory use
uptimeRunning time and load average
htopA friendlier top (extra package)
pstree -p | head
vmstat 5 3

10.10

Summary of the chapter

ps and top show processes; &, jobs, fg, bg control jobs; kill sends signals.

1Which signal does kill send when you give no signal?

TERM (15): "please end".

2How do you move a running foreground program to the background?

Ctrl-z to pause it, then bg.

3Which niceness gives a process the lowest priority?

19.

Practice

All commands in this chapter