Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

Hoofdstuk 10Naam in het boek: Managing Processes

Find, control and stop processes

Every running program is a process. This chapter shows how to run jobs in the background, find processes, change their priority, send signals to stop them, watch the system with top, and tune performance with tuned.

  • 15 Lessen
  • 24 Commando's
  • 1 Labs

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

Examendoelen in dit hoofdstuk

  • Processen vinden die veel CPU of geheugen gebruiken en ze stoppen
  • De planning (prioriteit) van processen aanpassen
  • Tuning-profielen beheren

10.1

Know the kinds of processes

There are shell jobs (started from your terminal) and daemons (services started by systemd). You manage them in different ways.

KindStarted byManaged with
Shell jobYou, in a terminaljobs, fg, bg, Ctrl+Z, Ctrl+C
Daemon / servicesystemd at bootsystemctl (next chapter)
Kernel threadThe kernelNot managed by you (names in [ ] in ps)

Every process has a PID (process ID) and a PPID (the PID of its parent).

10.2

Run jobs in the background

A command with & at the end runs in the background, so your terminal stays free.

sleep 600 &
[1] 4123
dd if=/dev/zero of=/dev/null &
jobs
[1]-  Running                 sleep 600 &
[2]+  Running                 dd if=/dev/zero of=/dev/null &
Key or commandDoes
COMMAND &Start in the background
Ctrl+ZPause (stop) the foreground job
bgContinue the paused job in the background
fg / fg %2Bring a job to the foreground
Ctrl+CStop (end) the foreground job
jobsList the jobs of this shell
  1. Start a long job in the foreground, pause it and send it to the background.
    sleep 300
    ^Z
    [1]+  Stopped                 sleep 300
    bg
    [1]+ sleep 300 &
  2. Bring it back and stop it.
    fg %1
    ^C

Let op

Jobs end when you close the terminal (they get a hang-up signal). For long tasks use nohup COMMAND &, tmux, or a systemd unit.

10.3

Understand parent and child processes

Every process is started by a parent. When the parent ends, the children usually end too — except daemons, which belong to systemd (PID 1).

ps -ef --forest | grep -A2 sshd
pstree -p | head -20
systemd-cgls --no-pager | head -30

10.4

Show processes with ps

ps takes a snapshot of the processes. Two styles are common: ps aux (BSD) and ps -ef (UNIX).

ps aux | head -5
USER   PID %CPU %MEM    VSZ   RSS TTY  STAT START   TIME COMMAND
root     1  0.0  0.4 174876 16420 ?    Ss   07:58   0:02 /usr/lib/systemd/systemd --switched-root --system
ps -ef | grep httpd
ps -fp 1010
ps -eo pid,ppid,user,%cpu,%mem,cmd --sort=-%mem | head
ColumnMeaning
PID / PPIDProcess ID / parent ID
%CPU, %MEMUse of CPU and memory
RSSReal memory in KiB
STATState: R running, S sleeping, D waiting for disk, T stopped, Z zombie
TIMETotal CPU time used

Onthoud

ps aux --sort=-%cpu | head = who uses the most CPU; --sort=-%mem = the most memory.

10.5

Know processes and threads

A process can have many threads that share its memory. The kernel schedules threads; ps and top can show them.

ps -eLf | grep chronyd
ps -o nlwp,cmd -p $(pidof chronyd)
top -H -p $(pidof chronyd)

10.6

Understand priorities and nice values

The nice value goes from -20 (highest priority) to 19 (lowest). A process with a higher nice value is "nicer" to others: it gets less CPU when the CPU is busy.

  • Normal processes start with nice 0.
  • Normal users may only make their processes nicer (0 → 19). Only root can lower the value (give more priority).
  • Real-time processes (shown as rt in top) always go before normal ones.

10.7

Change priorities

nice starts a command with a nice value; renice changes a running process.

nice -n 10 tar -czf /tmp/backup.tgz /home
nice -n -5 important-task
renice -n 5 -p 4123
renice -n -10 -p $(pidof myapp)
ps -o pid,ni,cmd -p 4123

Examentip

A task like "start the process with a lower priority" = nice -n 10 COMMAND. "Change the priority of a running process" = renice -n VALUE -p PID. In top, press r to renice.

10.8

Know slices and cgroups

systemd puts every process in a cgroup. Processes are grouped into slices: system.slice (services), user.slice (logged-in users) and machine.slice (VMs, containers). CPU is shared between the slices first.

systemd-cgls --no-pager | head -25
systemd-cgtop
systemctl status user.slice

Notitie

So one busy user cannot take all CPU from the services: the slices share it.

10.9

Send signals to processes

kill sends a signal to a process. The default signal asks the process to stop in a clean way.

SignalNumberMeaning
SIGTERM15Please stop (default, clean: saves data)
SIGKILL9Stop now (the kernel ends it; no cleanup)
SIGHUP1Re-read the config (many daemons) / terminal closed
SIGINT2Interrupt (= Ctrl+C)
SIGSTOP / SIGCONT19 / 18Pause / continue
kill 4123
kill -9 4123
kill -l
killall dd
pkill -u lisa
pkill -HUP sshd
pgrep -l -u apache

Op het werk

Always try kill PID (SIGTERM) first and wait a few seconds. kill -9 can leave broken files, locks and half-written data.

10.10

Deal with zombies

A zombie (state Z) is a process that ended, but its parent did not read the exit status yet. It uses no CPU or memory, only a place in the process table. You cannot kill a zombie — it is already dead.

ps -eo pid,ppid,stat,cmd | awk '$3 ~ /Z/'
ps -o ppid= -p ZOMBIE_PID
  1. Find the parent of the zombie (PPID).
  2. Ask the parent to clean up (kill -s SIGCHLD PARENT_PID) or restart the parent.
  3. When the parent ends, systemd (PID 1) takes the zombie and removes it.

10.11

Manage processes with top

top shows the processes live, sorted by CPU, with the load and memory at the top. You can also kill and renice from it.

top
top -u apache
top -b -n 1 | head -15
Key in topDoes
kKill: send a signal to a PID
rRenice a PID
M / PSort by memory / by CPU
1Show every CPU core
fChoose the columns
WSave the settings
qQuit

The header shows the load average (1, 5 and 15 minutes). A load higher than the number of CPU cores means processes are waiting.

uptime
 10:52:01 up  2:54,  2 users,  load average: 0.15, 0.10, 0.09
nproc
2
free -h

Notitie

In the %Cpu(s) line, wa is the time the CPU waits for disks. A high wa means slow storage, not a busy CPU.

10.12

Optimize performance with tuned

tuned sets many kernel and hardware settings at once with a profile for a type of system.

dnf install -y tuned
systemctl enable --now tuned
tuned-adm list
tuned-adm active
tuned-adm recommend
tuned-adm profile virtual-guest
tuned-adm off
ProfileFor
balancedDefault for desktops and laptops
throughput-performanceServers with high load
virtual-guestVirtual machines (default for RHEL VMs)
virtual-hostHosts that run VMs
powersaveSave energy
latency-performance, network-latencyLow latency

Examentip

"Set the recommended tuned profile" = tuned-adm recommend, then tuned-adm profile NAME. Check with tuned-adm active.

10.13

Remember the main points

The most important facts of this chapter, in short.

  • A command with & runs in the background; jobs, fg, bg and Ctrl+Z control shell jobs.
  • Every process has a PID and a parent (PPID); ps aux, ps -ef and pstree show them.
  • top shows live CPU and memory use; press k to kill, r to renice, M to sort by memory.
  • Nice values go from -20 (high priority) to 19 (low); nice -n 10 CMD starts low, renice -n 5 -p PID changes a running process. Only root can raise priority.
  • Signals: 15 SIGTERM asks a process to stop, 9 SIGKILL forces it, 1 SIGHUP often means "read the config again". Use kill, killall, pkill.
  • systemd puts processes in slices (cgroups) to share resources.
  • tuned sets performance profiles: tuned-adm recommend, tuned-adm profile NAME.

10.14

Learn the key terms

The words of this chapter in simple English.

TermMeaning
ProcessA running program with its own PID.
ThreadA part of a process that runs at the same time as other parts.
JobA process started from the current shell.
PID / PPIDProcess ID / ID of the parent process.
Nice valueThe priority of a process for the CPU (-20 to 19).
SignalA message to a process, like stop or reload.
ZombieA finished process whose parent did not read its exit status yet.
Load averageThe average number of processes waiting to run (1, 5, 15 minutes).
cgroup / sliceA group of processes that shares limits for CPU and memory.
tuned profileA set of system settings for one kind of work.

10.15

Check what you learned

Think about the answer first, then open it.

1How do you move a running command to the background?

Press Ctrl+Z to stop it, then type bg.

2How do you find the 5 processes that use the most memory?

ps aux --sort=-%mem | head -6 (or press M in top).

3How do you start a backup script with the lowest priority?
4How do you lower the priority of the running process 2345?
5Which signal do you send first to stop a process nicely?

SIGTERM (15), the default of kill.

6How do you stop all processes of user lisa?
7How do you choose the tuned profile that fits this system?
8What does a load average of 4.0 mean on a system with 2 CPUs?

On average 4 processes want the CPU, so it is overloaded (about 2 wait all the time).

Oefenen

Alle commando's in dit hoofdstuk