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 15Naam in de cursus: Monitoring and Managing Linux Processes

Monitor and control processes

Every running program is a process with a number (the PID), an owner, a state and a share of CPU and memory. When a server is slow, you must find the process that eats the CPU, and stop it in the right way. This chapter shows how to list processes, read their states, control jobs in the shell, send signals and watch the load.

  • 7 Lessen
  • 19 Commando's
  • 1 Labs

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

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

Vaardigheden in dit hoofdstuk

  • Processen vinden en stoppen

15.1

Know what a process is

A process is a program in memory. It has a PID, a parent (PPID), an owner (user), and a state.

echo $$
4321
ps -o pid,ppid,user,stat,cmd -p $$
    PID    PPID USER     STAT CMD
   4321    4320 student  Ss   -bash
pstree -p | head
systemd(1)─┬─NetworkManager(812)─┬─{NetworkManager}(830)
           ├─sshd(1022)───sshd(4318)───sshd(4320)───bash(4321)───pstree(4400)
  • PID 1 is systemd, the first process. All others are its children or grandchildren.
  • $$ is the PID of your own shell.
StateLetterMeaning
RunningROn the CPU or ready to run
SleepingSWaits for something (input, network, a timer) — most processes
UninterruptibleDWaits for the disk or NFS; can not be killed until the wait ends
StoppedTPaused (for example with Ctrl+Z)
ZombieZFinished, but the parent did not read its exit status yet

15.2

List processes with ps

ps shows a snapshot. ps aux shows all processes of all users; ps -ef is the same in another style.

ps
ps aux | head -5
USER   PID %CPU %MEM    VSZ   RSS TTY  STAT START   TIME COMMAND
root     1  0.0  0.4 174168 16840 ?    Ss   Oct25   0:04 /usr/lib/systemd/systemd --switched-root --system
root     2  0.0  0.0      0     0 ?    S    Oct25   0:00 [kthreadd]
ps -ef | grep -v grep | grep httpd
ps aux --sort=-%cpu | head -6
ps aux --sort=-%mem | head -6
ps -u apache -o pid,%cpu,%mem,cmd
ps -eo pid,user,ni,stat,lstart,cmd --sort=-pcpu | head
pgrep -a sshd
ColumnMeaning
%CPU / %MEMShare of CPU / of memory
RSSReal memory in use (KiB)
TTYTerminal (? = no terminal: a service)
STATState (s = session leader, + = foreground, < / N = high / low priority)
TIMECPU time used so far
[name]A kernel thread

15.3

Watch live with top

top shows the processes live, sorted by CPU, with the load and the memory at the top.

top
top -u apache
top -o %MEM
top -b -n 1 | head -15
Key in topWhat it does
P / MSort by CPU / by memory
kKill: type the PID, then the signal
rRenice: change the priority
1Show every CPU core
uOnly one user
qQuit

Op het werk

top -b -n 1 (batch mode) prints one screen and exits — good for a log or a ticket.

15.4

Read the load average

The load average is the number of processes that want the CPU (or wait for the disk), as an average over 1, 5 and 15 minutes.

uptime
 11:42:01 up 2 days,  5:23,  2 users,  load average: 3.85, 2.10, 1.05
nproc
4
lscpu | grep '^CPU(s):'
CPU(s):              4

Compare the load with the number of CPU cores. On 4 cores, a load of 4.0 means "fully busy". Here 3.85 (1 min) is higher than 1.05 (15 min): the load is growing.

Onthoud

Load / cores below 1 = fine. About 1 = full. Clearly above 1 = processes wait. A high load with a low %CPU often means processes wait for a slow disk or NFS (state D).

15.5

Control jobs in the shell

A shell can run commands in the background (&) and move them between background and foreground.

sleep 600 &
[1] 4512
sleep 900
^Z
[2]+  Stopped                 sleep 900
jobs
[1]-  Running                 sleep 600 &
[2]+  Stopped                 sleep 900
bg %2
fg %1
^C
nohup ./long-backup.sh > backup.out 2>&1 &
Command / keyWhat it does
CMD &Start in the background
Ctrl+ZStop (pause) the foreground job
jobsList the jobs of this shell
bg %N / fg %NGo on in the background / bring to the foreground
Ctrl+CEnd the foreground job
nohup CMD &Keep running after you log out

Notitie

Jobs belong to one shell. A long task on a server is better in tmux, in a systemd service, or with systemd-run.

15.6

Stop processes with signals

kill sends a signal to a process. Most signals ask it to stop; the program can clean up first. SIGKILL stops it at once, with no clean-up.

SignalNumberMeaning
SIGHUP1"Hang up" — many services read their config again
SIGINT2Interrupt, same as Ctrl+C
SIGKILL9Kill at once; can not be caught or ignored
SIGTERM15Please end (the default of kill)
SIGCONT / SIGSTOP18 / 19Continue / pause
kill 4512
kill -15 4512
kill -9 4512
kill -l
pkill -u anna sleep
pkill -HUP rsyslogd
killall -s TERM httpd
pkill -KILL -t pts/2
kill %1

Onthoud

First kill PID (TERM). Wait a few seconds. Only if it still runs, use kill -9 PID. With -9 the program can not save data or remove its lock files.

Let op

pkill name matches part of the name: pkill ssh also kills sshd and every SSH session. Check first with pgrep -a name.

15.7

Log out other users

To end the session of another user, kill the processes on his terminal or of his user.

w
pkill -t pts/1
pkill -KILL -u omar
loginctl list-sessions
loginctl terminate-user omar

Op het werk

Before you end somebody's session, use w to see what he runs — maybe a long update that must not be stopped.

Oefenen

Alle commando's in dit hoofdstuk