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
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.
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 command | Does |
|---|---|
COMMAND & | Start in the background |
| Ctrl+Z | Pause (stop) the foreground job |
bg | Continue the paused job in the background |
fg / fg %2 | Bring a job to the foreground |
| Ctrl+C | Stop (end) the foreground job |
jobs | List the jobs of this shell |
- Start a long job in the foreground, pause it and send it to the background.
- 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.
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).
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| Column | Meaning |
|---|---|
PID / PPID | Process ID / parent ID |
%CPU, %MEM | Use of CPU and memory |
RSS | Real memory in KiB |
STAT | State: R running, S sleeping, D waiting for disk, T stopped, Z zombie |
TIME | Total CPU time used |
Onthoud
ps aux --sort=-%cpu | head = who uses the most CPU; --sort=-%mem = the most memory.
Know processes and threads
A process can have many threads that share its memory. The kernel schedules threads; ps and top can show them.
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
rtin top) always go before normal ones.
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 4123Examentip
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.
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.sliceNotitie
So one busy user cannot take all CPU from the services: the slices share it.
Send signals to processes
kill sends a signal to a process. The default signal asks the process to stop in a clean way.
| Signal | Number | Meaning |
|---|---|---|
SIGTERM | 15 | Please stop (default, clean: saves data) |
SIGKILL | 9 | Stop now (the kernel ends it; no cleanup) |
SIGHUP | 1 | Re-read the config (many daemons) / terminal closed |
SIGINT | 2 | Interrupt (= Ctrl+C) |
SIGSTOP / SIGCONT | 19 / 18 | Pause / continue |
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.
- Find the parent of the zombie (PPID).
- Ask the parent to clean up (
kill -s SIGCHLD PARENT_PID) or restart the parent. - When the parent ends, systemd (PID 1) takes the zombie and removes it.
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.
| Key in top | Does |
|---|---|
k | Kill: send a signal to a PID |
r | Renice a PID |
M / P | Sort by memory / by CPU |
1 | Show every CPU core |
f | Choose the columns |
W | Save the settings |
q | Quit |
The header shows the load average (1, 5 and 15 minutes). A load higher than the number of CPU cores means processes are waiting.
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.
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| Profile | For |
|---|---|
balanced | Default for desktops and laptops |
throughput-performance | Servers with high load |
virtual-guest | Virtual machines (default for RHEL VMs) |
virtual-host | Hosts that run VMs |
powersave | Save energy |
latency-performance, network-latency | Low latency |
Examentip
"Set the recommended tuned profile" = tuned-adm recommend, then tuned-adm profile NAME. Check with tuned-adm active.
Remember the main points
The most important facts of this chapter, in short.
- A command with
&runs in the background;jobs,fg,bgand Ctrl+Z control shell jobs. - Every process has a PID and a parent (PPID);
ps aux,ps -efandpstreeshow them. topshows live CPU and memory use; presskto kill,rto renice,Mto sort by memory.- Nice values go from -20 (high priority) to 19 (low);
nice -n 10 CMDstarts low,renice -n 5 -p PIDchanges 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.
Learn the key terms
The words of this chapter in simple English.
| Term | Meaning |
|---|---|
| Process | A running program with its own PID. |
| Thread | A part of a process that runs at the same time as other parts. |
| Job | A process started from the current shell. |
| PID / PPID | Process ID / ID of the parent process. |
| Nice value | The priority of a process for the CPU (-20 to 19). |
| Signal | A message to a process, like stop or reload. |
| Zombie | A finished process whose parent did not read its exit status yet. |
| Load average | The average number of processes waiting to run (1, 5, 15 minutes). |
| cgroup / slice | A group of processes that shares limits for CPU and memory. |
| tuned profile | A set of system settings for one kind of work. |
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?
pkill -u lisa (or killall -u lisa).
7How do you choose the tuned profile that fits this system?
tuned-adm recommend, then tuned-adm profile NAME; check with tuned-adm active.
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).