Chapter 8

MONITORING AND MANAGING LINUX PROCESSES

PS

RHEL9-da ən birinci başlayan proses systemd prosesidir. Bütün digər proseslər systemd-nin child prosesləridir. Hər prosesin İD-si, hər child prosesin isə PPİD (parent's process ID)-si olur.

Parent prosesdən child proses yaranarkən, child proses parentin bütün özəlliklərini (environment variables, səlahiyyətlər, current file descriptorları, portları və s.) məlumtları inherit edir. Child proses yarandıqda parent proses wait əmriylə gözləmə rejiminə keçir. Child proses öz işini bitirdikdən sonra özünə aid olan bir çox resusları silsə də, process table-da zombie proses olaraq onun entry-si qalmaqda davam edir.

Child process exit etdikdən sonra parent process process table-da child-in zombie entrysini silir və öz fəaliyyətinə davam edir.

Linuxda proseslərə baxmaq aşağıdakı yollarla mümkündür:

# student istifadəçisinə aid olan bütün prosesləri göstərir

[student@localhost ~]$ pgrep -l -u student 
1445 systemd
1447 (sd-pam)
1454 sshd
1455 bash
1479 ping
1480 sort
1485 bash

# student istifadəçisinə aid olan bütün prosesləri tree formasında göstərir

[student@localhost ~]$ pstree -p student 
bash(1485)───su(1569)───bash(1573)

sshd(1454)───bash(1455)─┬─ping(1479)
                        ├─pstree(1627)
                        └─sort(1480)
[student@localhost ~]$ top

Tasks: 213 total,   1 running, 212 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.0 sy,  0.0 ni, 99.8 id,  0.0 wa,  0.2 hi,  0.0 si,  0.0 st
MiB Mem :   3635.0 total,   3124.1 free,    516.2 used,    220.6 buff/cache
MiB Swap:   4020.0 total,   4020.0 free,      0.0 used.   3118.8 avail Mem 

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND                                         
   1921 student   20   0   10688   4220   3356 R   0.7   0.1   0:00.02 top                                             
      1 root      20   0  171956  17828  10500 S   0.0   0.5   0:01.97 systemd                                         
      2 root      20   0       0      0      0 S   0.0   0.0   0:00.01 kthreadd                                        
      3 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_gp                                          
      4 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_par_gp                                      
      5 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 slub_flushwq                                    
      6 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 netns                                           
      8 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 kworker/0:0H-events_highpri
[student@localhost ~]$ sudo ps aux | head

USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.0  0.4 171956 17828 ?        Ss   11:59   0:01 /usr/lib/systemd/systemd --switched-root --system --deserialize 31
root           2  0.0  0.0      0     0 ?        S    11:59   0:00 [kthreadd]
root           3  0.0  0.0      0     0 ?        I<   11:59   0:00 [rcu_gp]
root           4  0.0  0.0      0     0 ?        I<   11:59   0:00 [rcu_par_gp]
root           5  0.0  0.0      0     0 ?        I<   11:59   0:00 [slub_flushwq]
root           6  0.0  0.0      0     0 ?        I<   11:59   0:00 [netns]
root           8  0.0  0.0      0     0 ?        I<   11:59   0:00 [kworker/0:0H-events_highpri]
root          10  0.0  0.0      0     0 ?        I<   11:59   0:00 [kworker/0:1H-events_highpri]
root          11  0.0  0.0      0     0 ?        I<   11:59   0:00 [mm_percpu_wq]

sudo vasitəsilə ps işlətdikdə TTY sual işarəsi görsənməsinin səbəbi, bəzi proseslər system daemon tərəfindən başladıldığı üçün onların heç bir TTY-ə aid olmamasıdır.

top komandası vasitəsilə S sütununa baxdıqda proses state-ləri görmək mümkündür.

ps -aux komandası vasitəsilə STAT sütununa baxdıqda stateləri görmək mümkündür. PS komandası outputu 3 fərqli formatda verə bilir (GNU, POSİX və BSD). Bu səbəbdən ps arqumentində - işlətmək outputu dəyişir. Məsələn, ps -efps ef eyni outputu verməyəcək.

State
FLAG
Description

Running

R

TASK_RUNNING:

Proses CPU üzərində fəaliyyətdədir, ya da fəaliyyətə düşməyi gözləyir.

Process can be executing user routines or kernel routines (system calls), or be queued and ready when in the Running (or Runnable) state.

Sleeping

S

TASK_INTERRUPTIBLE:

Proses üçündən birindən cavab almaqçün gözləyir:

Hardware request

System resource access

Signal

Şərait təmin olunduqda, proses Running state-ə geri qayıdacaq.

D

TASK_UNINTERRUPTIBLE:

D flagının S-dən fərqi ondadır ki, heç bir siqnala cavab vermir.

Yalnız prosesin dayanması cihazı gözlənilməz vəziyyətə gətirib çıxarma ehtimalı varsa istifadə olunur.

K

TASK_KILLABLE:

D ilə eynidir, sadəcə exception kimi fatal signallara KİLL siqnalına (tamamilə prosesdi sonlandırma) cavab verir.

Utilities frequently display Killable processes as D state.

I

TASK_REPORT_IDLE:

Kernel threadları üçün istifadə olunur.

A subset of state D. The kernel does not count these processes when calculating load average. Used for kernel threads. Flags TASK_UNINTERRUPTABLE and TASK_NOLOAD are set. Similar to TASK_KILLABLE, also a subset of state D. It accepts fatal signals.

Stopped

T

TASK_STOPPED:

İstifadəçi vəya digər proses tərəfindən proses dayandırılmışdır (stopped/suspended).

Proses resume edilərək Running state-ə qaytarıla bilər.

T

TASK_TRACED:

Proses hal-hazırda debug olunur və müvəqqəti olaraq dayandırılmışdır (stopped).

Zombie

Z

EXIT_ZOMBIE:

Child proses parentə exit olması haqqında signal verir. PID istisna olmaqla digər bütün resurslar silinir.

X

EXIT_DEAD:

Parent, child prosesin geridə qalmış proses strukturlarını sildikdən sonra bu state-ə gəlir. Bu state heç vaxt process-listing alətlərində görsənmir.

Jobs

Eyni terminal session-dan bir neçə prosesi idarə etmək üçün yaradılmış funksionallıqdır.

# Jobs komandasından istifadə edərək hal-hazırda neçə job-un olduğuna baxırıq
[student@localhost ~]$ jobs

# ps j vasitəsilə joba aid olan prosesləri list edirəm
[student@localhost ~]$ ps j
   PPID     PID    PGID     SID TTY        TPGID STAT   UID   TIME COMMAND
   1454    1455    1455    1455 pts/0       1476 Ss    1000   0:00 -bash
   1455    1476    1476    1455 pts/0       1476 R+    1000   0:00 ps j

# Nümunə üçün komanda və pipe yazıb onu & işarəsi vasitəsilə backgrounda göndərirəm.
# Diqqət: Ekranda görünən PİD ping-in yox sort-un PİD-sidir.
[student@localhost ~]$ ping 1.1.1.1 | sort &
[1] 1480

[student@localhost ~]$ jobs
[1]+  Running                 ping 1.1.1.1 | sort &

[student@localhost ~]$ ps j
   PPID     PID    PGID     SID TTY        TPGID STAT   UID   TIME COMMAND
   1454    1455    1455    1455 pts/0       1481 Ss    1000   0:00 -bash
   1455    1479    1479    1455 pts/0       1481 S     1000   0:00 ping 1.1.1.1
   1455    1480    1479    1455 pts/0       1481 S     1000   0:00 sort
   1455    1481    1481    1455 pts/0       1481 R+    1000   0:00 ps j

# fg əmri vasitəsilə %n-ci prosesi terminala qaytarıram.
[student@localhost ~]$ fg %1
ping 1.1.1.1 | sort

# Klavyaturada CTRL+Z basaraq foregrounddakı jobu suspend(stop) edirəm.
^Z
[1]+  Stopped                 ping 1.1.1.1 | sort

# ps j vasitəsilə görürəm ki, process state (STAT) T vəziyyətindədir.
[student@localhost ~]$ ps j
   PPID     PID    PGID     SID TTY        TPGID STAT   UID   TIME COMMAND
   1454    1455    1455    1455 pts/0       1521 Ss    1000   0:00 -bash
   1455    1479    1479    1455 pts/0       1521 T     1000   0:00 ping 1.1.1.1
   1455    1480    1479    1455 pts/0       1521 T     1000   0:00 sort
    827    1485    1485    1485 tty1        1485 Ss+   1000   0:00 -bash
   1455    1521    1521    1455 pts/0       1521 R+    1000   0:00 ps j

# bg %1 vasitəsilə 1 saylı jobu axra fonda (background) işlədirəm.
[student@localhost ~]$ bg %1
[1]+ ping 1.1.1.1 | sort &

# ps j vasitəsilə görürəm ki, həm ping həm sort-un state-i(STAT) S-ə çevrilmişdir.
[student@localhost ~]$ ps j
   PPID     PID    PGID     SID TTY        TPGID STAT   UID   TIME COMMAND
   1454    1455    1455    1455 pts/0       1525 Ss    1000   0:00 -bash
   1455    1479    1479    1455 pts/0       1525 S     1000   0:00 ping 1.1.1.1
   1455    1480    1479    1455 pts/0       1525 S     1000   0:00 sort
    827    1485    1485    1485 tty1        1485 Ss+   1000   0:00 -bash
   1455    1525    1525    1455 pts/0       1525 R+    1000   0:00 ps j

kill

1

HUP

Hangup

Used to report termination of the controlling process of a terminal. Also used to request process reinitialization (configuration reload) without termination.

2

INT

Keyboard interrupt

Causes program termination. Can be blocked or handled. Sent by pressing INTR key combination (Ctrl+C).

3

QUIT

Keyboard quit

Similar to SIGINT, but also produces a process dump at termination. Sent by pressing QUIT key combination (Ctrl+\).

9

KILL

Kill, unblockable

Causes abrupt program termination. Cannot be blocked, ignored, or handled; always fatal.

15

default

TERM

Terminate

Causes program termination. Unlike SIGKILL, can be blocked, ignored, or handled. The “polite” way to ask a program to terminate; allows self-cleanup.

18

CONT

Continue

Sent to a process to resume, if stopped. Cannot be blocked. Even if handled, always resumes the process.

19

STOP

Stop, unblockable

Suspends the process. Cannot be blocked or handled.

20

TSTP

Keyboard stop

Unlike SIGSTOP, can be blocked, ignored, or handled. Sent by pressing SUSP key combination (Ctrl+Z).

Hər bir signalın default actionu var:

1) Term - prosesi terminate (exit) edir

2) Core - prosesin memory imageini dump edir, sonra exit edir

3) Stop - prosesi (stop) edir, və davam etmək üçün (resume) gözləyir.

Signal nömrələri fərqli linux distrolarında müxtəlifdir, signal adları isə standartlaşdırılmışdır, bu səbəbdən komandaların icrasında signal nömrələrini yox adları yazmaq daha məqsədəuyğundur.

Məsələn, arxa fonda işləyən jobu aşağıdakı qaydada STOP etmək mümkündür:

[student@localhost ~]$ jobs
[1]+  Running                 ping 1.1.1.1 | sort &

[student@localhost ~]$ kill -STOP %1

[student@localhost ~]$ jobs
[1]+  Stopped                 ping 1.1.1.1 | sort

Killall pkill-ə nisbətən daha aqresiv komandadır. Hər iki komanda prosesin PİD-sini yox adını bildikdə, onu sonlandırmaq üçün istifadəyə yarayır.



pkill firefox
pkill -9 firefox
pkill -u username    # istifadəçi tərəfindən başladılmış bütün prosesləri dayandırır
killall firefox
killall -9 firefox
killall -u username  # istifadəçi tərəfindən başladılmış bütün prosesləri dayandırır

Remotely logging out users

İstifadəçilərin hansı tty-dən istifadə etdiyini aydınlaşdırmaq üçün w əmrini icra edirik.

tty/n - terminal device

pts/n - pseudo-terminal associated with a graphical terminal window or remote login session

[student@localhost ~]$ w
16:01:31 up  1:59,  2 users,  load average: 0.00, 0.02, 0.07
USER     TTY        LOGIN@   IDLE   JCPU   PCPU WHAT
student  tty1      14:04   43.00s  0.00s  0.01s login -- student        
student  pts/0     14:03    2.00s  0.19s  0.00s w

Burada JCPU istifadəçinin background joblarının CPU istifadəsini, PCPU isə foreground jobların CPU istifadəsini göstərir.

Birinci SİGTERM, sonra SİGİNT, sonra SİGKİLL əmrləri göndərilməlidir. Çünki proseslər birinci SİGKİLL ilə kill edilərsə, onlar adətən self-cleanup tədbirləri görmürlər.

MONITORING PROCESS ACTIVITY

uptime
lscpu
top - 20:36:47 up 2 min,  1 user,  load average: 0.15, 0.15, 0.07
Tasks: 243 total,   1 running, 242 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.2 us,  0.2 sy,  0.0 ni, 99.3 id,  0.0 wa,  0.3 hi,  0.0 si,  0.0 st
MiB Mem :   3635.0 total,   3190.4 free,    477.5 used,    176.3 buff/cache
MiB Swap:   4020.0 total,   4020.0 free,      0.0 used.   3157.5 avail Mem 

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND                                         
     54 root      20   0       0      0      0 I   0.3   0.0   0:00.18 kworker/1:3-events                              
   1522 student   20   0   10724   4232   3368 R   0.3   0.1   0:00.25 top                                             
      1 root      20   0  106276  17792  10540 S   0.0   0.5   0:01.84 systemd                                         
      2 root      20   0       0      0      0 S   0.0   0.0   0:00.01 kthreadd                                        
      3 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_gp                                                                             
  1. PID:

    • The Process ID number of the process.

  2. USER:

    • The username of the user who owns the process.

  3. PR:

    • Priority of the process. This is the priority with which the kernel schedules the process.

  4. NI:

    • The nice value of the process, which is a user-space concept used to influence the scheduling priority. Nice values range from -20 (highest priority) to 19 (lowest priority).

  5. VIRT:

    • Virtual Memory Size of the process, which includes all code, data and shared libraries plus pages that have been swapped out and pages that have been mapped but not used.

  6. RES:

    • Resident Set Size: the non-swapped physical memory a task has used.

  7. SHR:

    • Shared memory size: The amount of shared memory used by a task. It reflects memory shared with other processes.

  8. S:

    • Process Status:

      • D is for uninterruptible sleep (usually IO).

      • R is for running.

      • S is for sleeping.

      • T is for traced or stopped.

      • Z is for zombie.

  9. %CPU:

    • The percentage of the CPU time that the task has received recently.

  10. %MEM:

    • The percentage of the total memory used by the process.

  11. TIME+:

    • The total CPU time the task has used since it started.

  12. COMMAND:

    • The command that initiated the process.

KEY

PURPOSE

?orH

Help for interactive keystrokes.

L,T,M

Toggles for load, threads, and memory header lines.

1

Toggle showing individual CPUs or a summary for all CPUs in header.

S (1)

Change the refresh (screen) rate, in decimal seconds (e.g., 0.5, 1, 5).

B

Toggle reverse highlighting for Running processes; default is bold only.

B

Enables use of bold in display, in the header, and for Running processes.

Shift+H

Toggle threads; show process summary or individual threads.

U, Shift+U

Filter for any user name (effective, real).

Shift+M

Sorts process listing by memory usage, in descending order.

Shift+P

Sorts process listing by processor utilization, in descending order.

K (1)

Kill a process. When prompted, enter PID, then signal.

R (1)

Renice a process. When prompted, enter PID, then nice_value.

Shift+W

Write (save) the current display configuration for use at the next top restart.

Q

Quit.

Last updated

Was this helpful?