Linux-admin-process-management

提供:Dev Guides
移動先:案内検索

Linux Admin-プロセス管理

以下は、Process Managementで使用される一般的なコマンドです。bg、fg、nohup、ps、pstree、top、kill、killall、free、uptime、nice。

プロセスを操作する

クイックノート:LinuxでのPIDの処理

Linuxでは、実行中のすべてのプロセスにPIDまたはプロセスID番号が付与されます。 この_PID_は、CentOSが特定のプロセスを識別する方法です。 前述したように、_systemd_は、CentOSで最初に開始され、PIDが1のプロセスです。

*Pgrep* は、特定のプロセス名のLinux PIDを取得するために使用されます。
[root@CentOS]# pgrep systemd
1
[root@CentOS]#

ご覧のとおり、_pgrep_コマンドはsystemdの現在のPIDを返します。

CentOSの基本的なCentOSプロセスとジョブ管理

Linuxでプロセスを操作する場合、コマンドラインで基本的なフォアグラウンド処理とバックグラウンド処理がどのように実行されるかを知ることが重要です。

  • fg -プロセスをフォアグラウンドにします
  • bg -プロセスをバックグラウンドに移動します
  • jobs -シェルに接続されている現在のプロセスのリスト
  • ctrl + z -現在のプロセスをスリープさせるためのCtrl + zキーの組み合わせ
  • -バックグラウンドでプロセスを開始します

シェルコマンド_sleep_の使用を開始しましょう。 sleep は、指定された時間だけスリープします:sleep

[root@CentOS ~]$ jobs

[root@CentOS ~]$ sleep 10 &
[1] 12454

[root@CentOS ~]$ sleep 20 &
[2] 12479

[root@CentOS ~]$ jobs
[1]-  Running                 sleep 10 &
[2]+  Running                 sleep 20 &

[cnetos@CentOS ~]$

今、最初の仕事を手前に持って行きましょう-

[root@CentOS ~]$ fg 1
sleep 10

フォローしている場合、フォアグラウンドジョブがシェルでスタックしていることに気付くでしょう。 次に、プロセスをスリープ状態にしてから、バックグラウンドで再度有効にします。

  • Ctrl + Zを押す
  • bg 1と入力して、最初のジョブをバックグラウンドに送信して開始します。
[root@CentOS ~]$ fg 1
sleep 20
^Z
[1]+  Stopped                 sleep 20

[root@CentOS ~]$ bg 1
[1]+ sleep 20 &

[root@CentOS ~]$

ノープ

シェルまたはターミナルから作業する場合、デフォルトでは、シェルが閉じられるか、ユーザーがログアウトすると、シェルに接続されているすべてのプロセスとジョブが終了することに注意してください。 _nohup_を使用している場合、ユーザーがログアウトするか、プロセスが接続されているシェルを閉じると、プロセスは引き続き実行されます。

[root@CentOS]# nohup ping www.google.com &
[1] 27299
nohup: ignoring input and appending output to ‘nohup.out’

[root@CentOS]# pgrep ping
27299

[root@CentOS]# kill -KILL `pgrep ping`
[1]+  Killed                  nohup ping www.google.com

[root@CentOS rdc]# cat nohup.out
PING www.google.com (216.58.193.68) 56(84) bytes of data.
64 bytes from sea15s07-in-f4.1e100.net (216.58.193.68): icmp_seq = 1 ttl = 128
time = 51.6 ms
64 bytes from sea15s07-in-f4.1e100.net (216.58.193.68): icmp_seq = 2 ttl = 128
time = 54.2 ms
64 bytes from sea15s07-in-f4.1e100.net (216.58.193.68): icmp_seq = 3 ttl = 128
time = 52.7 ms

psコマンド

*ps* コマンドは、特定のプロセスのスナップショットを調査するために管理者によって一般的に使用されます。 _ps_は一般に_grep_とともに使用され、分析する特定のプロセスを除外します。
[root@CentOS ~]$ ps axw | grep python
762   ?        Ssl    0:01/usr/bin/python -Es/usr/sbin/firewalld --nofork -nopid
1296  ?        Ssl    0:00/usr/bin/python -Es/usr/sbin/tuned -l -P
15550 pts/0    S+     0:00 grep --color=auto python

上記のコマンドでは、_python_インタープリターを使用するすべてのプロセスが表示されます。 また、文字列_python_を探すgrepコマンドも結果に含まれていました。

以下は、_ps_で使用される最も一般的なコマンドラインスイッチです。

Switch Action
a Excludes constraints of only the reporting processes for the current user
x Shows processes not attached to a tty or shell
w Formats wide output display of the output
e Shows environment after the command
-e Selects all processes
-o User-defined formatted output
-u Shows all processes by a specific user
-C Shows all processes by name or process id
--sort Sorts the processes by definition

_nobody_ユーザーが使用中のすべてのプロセスを表示するには-

[root@CentOS ~]$ ps -u nobody
PID TTY          TIME CMD
1853 ?        00:00:00 dnsmasq

[root@CentOS ~]$

_firewalld_プロセスに関するすべての情報を表示するには-

[root@CentOS ~]$ ps -wl -C firewalld
F   S   UID   PID   PPID   C   PRI   NI   ADDR   SZ   WCHAN   TTY   TIME      CMD
0   S     0   762      1   0    80   0     -   81786  poll_s   ?   00:00:01 firewalld

[root@CentOS ~]$

どのプロセスが最もメモリを消費しているかを見てみましょう-

[root@CentOS ~]$ ps aux  --sort=-pmem | head -10
USER       PID   %CPU   %MEM   VSZ     RSS   TTY   STAT   START   TIME   COMMAND
cnetos     6130   0.7   5.7   1344512 108364  ?      Sl   02:16   0:29 /usr/bin/gnome-shell
cnetos     6449   0.0   3.4   1375872 64440   ?      Sl   02:16   0:00 /usr/libexec/evolution-calendar-factory
root       5404   0.6   2.1   190256  39920 tty1     Ssl+ 02:15   0:27 /usr/bin/Xorg :0 -background none -noreset -audit 4 -verbose -auth/run/gdm/auth-for-gdm-iDefCt/database -seat seat0 -nolisten tcp vt1
cnetos     6296   0.0   1.7   1081944 32136   ?      Sl   02:16   0:00 /usr/libexec/evolution/3.12/evolution-alarm-notify
cnetos     6350   0.0   1.5   560728  29844   ?      Sl   02:16   0:01 /usr/bin/prlsga
cnetos     6158   0.0   1.4   1026956 28004   ?      Sl   02:16   0:00 /usr/libexec/gnome-shell-calendar-server
cnetos     6169   0.0   1.4   1120028 27576   ?      Sl   02:16   0:00 /usr/libexec/evolution-source-registry
root       762    0.0   1.4   327144  26724   ?      Ssl  02:09   0:01 /usr/bin/python -Es/usr/sbin/firewalld --nofork --nopid
cnetos     6026   0.0  1.4 1090832 26376      ?      Sl   02:16   0:00 /usr/libexec/gnome-settings-daemon

[root@CentOS ~]$

ユーザーの中心と形式ごとにすべてのプロセスを確認し、カスタム出力を表示します-

[cnetos@CentOS ~]$ ps -u cnetos -o pid,uname,comm
   PID    USER     COMMAND
   5802  centos   gnome-keyring-d
   5812  cnetos   gnome-session
   5819  cnetos   dbus-launch
   5820  cnetos   dbus-daemon
   5888  cnetos   gvfsd
   5893  cnetos   gvfsd-fuse
   5980  cnetos   ssh-agent
   5996  cnetos   at-spi-bus-laun

pstreeコマンド

*pstree* は_ps_に似ていますが、あまり使用されません。 きれいなツリー形式でプロセスを表示します。
[centos@CentOS ~]$ pstree
  systemd─┬─ModemManager───2*[{ModemManager}]
          ├─NetworkManager─┬─dhclient
          │                └─2*[{NetworkManager}]
          ├─2*[abrt-watch-log]
          ├─abrtd
          ├─accounts-daemon───2*[{accounts-daemon}]
          ├─alsactl
          ├─at-spi-bus-laun─┬─dbus-daemon───{dbus-daemon}
          │                 └─3*[{at-spi-bus-laun}]
          ├─at-spi2-registr───2*[{at-spi2-registr}]
          ├─atd
          ├─auditd─┬─audispd─┬─sedispatch
          │        │         └─{audispd}
          │        └─{auditd}
          ├─avahi-daemon───avahi-daemon
          ├─caribou───2*[{caribou}]
          ├─cgrulesengd
          ├─chronyd
          ├─colord───2*[{colord}]
          ├─crond
          ├─cupsd

_pstree_からの合計出力は100行を超えることがあります。 通常、_ps_はより有用な情報を提供します。

topコマンド

*top* は、Linuxのパフォーマンスの問題をトラブルシューティングするときに最も頻繁に使用されるコマンドの1つです。 Linuxでのリアルタイムの統計とプロセスの監視に役立ちます。 以下は、コマンドラインから起動した場合の_top_のデフォルト出力です。
Tasks: 170 total,   1 running, 169 sleeping,   0 stopped,   0 zombie
%Cpu(s):  2.3 us,  2.0 sy,  0.0 ni, 95.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1879668 total,   177020 free,   607544 used,  1095104 buff/cache
KiB Swap:  3145724 total,  3145428 free,      296 used.  1034648 avail Mem

PID    USER     PR   NI    VIRT     RES   SHR    S  %CPU  %MEM   TIME+   COMMAND
5404   root     20   0    197832   48024  6744   S   1.3   2.6  1:13.22   Xorg
8013   centos   20   0    555316   23104  13140  S   1.0   1.2  0:14.89   gnome-terminal-
6339   centos   20   0    332336   6016   3248   S   0.3   0.3  0:23.71   prlcc
6351   centos   20   0    21044    1532   1292   S   0.3   0.1  0:02.66   prlshprof

topの実行中に使用される一般的な_hot keys_(_hot keys_は、シェルでtopが実行されているときにキーを押すことによりアクセスされます)。

Command Action
b Enables/disables bold highlighting on top menu
z Cycles the color scheme
l Cycles the load average heading
m Cycles the memory average heading
t Task information heading
h Help menu
Shift+F Customizes sorting and display fields

以下は、_top_の一般的なコマンドラインスイッチです。

Command Action
-o Sorts by column (can prepend with - or + to sort ascending or descending)
-u Shows only processes from a specified user
-d Updates the delay time of top
-O Returns a list of columns which top can apply sorting

_Shift + F_を使用して表示される上部の並べ替えオプション画面。 この画面では、_top_表示および並べ替えオプションをカスタマイズできます。

Fields Management for window 1:Def, whose current sort field is %MEM
Navigate with Up/Dn, Right selects for move then <Enter> or Left commits,
 'd' or <Space> toggles display, 's' sets sort.  Use 'q' or <Esc> to end!

* PID     = Process Id             TGID    = Thread Group Id
* USER    = Effective User Name    ENVIRON = Environment vars
* PR      = Priority               vMj     = Major Faults delta
* NI      = Nice Value             vMn     = Minor Faults delta
* VIRT    = Virtual Image (KiB)    USED    = Res+Swap Size (KiB)
* RES     = Resident Size (KiB)    nsIPC   = IPC namespace Inode
* SHR     = Shared Memory (KiB)    nsMNT   = MNT namespace Inode
* S       = Process Status         nsNET   = NET namespace Inode
* %CPU    = CPU Usage              nsPID   = PID namespace Inode
* %MEM    = Memory Usage (RES)     nsUSER  = USER namespace Inode
* TIME+   = CPU Time, hundredths   nsUTS   = UTS namespace Inode
* COMMAND = Command Name/Line
PPID    = Parent Process pid
UID     = Effective User Id

top、ユーザー_rdc_のプロセスを表示し、メモリ使用量でソート-

 PID   USER  %MEM  PR  NI    VIRT    RES    SHR    S %CPU     TIME+    COMMAND
 6130  rdc    6.2  20   0  1349592  117160  33232  S  0.0   1:09.34    gnome-shell
 6449  rdc    3.4  20   0  1375872   64428  21400  S  0.0   0:00.43    evolution-calen
 6296  rdc    1.7  20   0  1081944   32140  22596  S  0.0   0:00.40    evolution-alarm
 6350  rdc    1.6  20   0   560728   29844   4256  S  0.0   0:10.16    prlsga
 6281  rdc    1.5  20   0  1027176   28808  17680  S  0.0   0:00.78    nautilus
 6158  rdc    1.5  20   0  1026956   28004  19072  S  0.0   0:00.20    gnome-shell-cal

有効なトップフィールドの表示(凝縮)-

[centos@CentOS ~]$ top -O
PID
PPID
UID
USER
RUID
RUSER
SUID
SUSER
GID
GROUP
PGRP
TTY
TPGID

killコマンド

*kill* コマンドは、PIDを介してコマンドシェルからプロセスを強制終了するために使用されます。 プロセスを_killing_する場合、送信するシグナルを指定する必要があります。 シグナルは、プロセスを終了する方法をカーネルに知らせます。 最も一般的に使用される信号は-
  • SIGTERM は、カーネルがプロセスに停止する必要があることを知らせるために暗示されています。 _SIGTERM_は、プロセスが正常に終了し、安全な終了操作を実行する機会を提供します。
  • SIGHUP ほとんどのデーモンは、_SIGHUP_を送信すると再起動します。 これは、構成ファイルに変更が加えられたときにプロセスでよく使用されます。
  • SIGKILL は、_SIGTERM_がプロセスのシャットダウンを要求するのと同等であるためです。 カーネルには、要求に準拠しないプロセスを終了するオプションが必要です。 プロセスがハングすると、_SIGKILL_オプションを使用してプロセスを明示的にシャットダウンします。

kill_で送信できるすべてのシグナルのリストについては、- l_オプションを使用できます-

[root@CentOS]# kill -l
1) SIGHUP           2) SIGINT         3) SIGQUIT        4) SIGILL         5) SIGTRAP
6) SIGABRT          7) SIGBUS         8) SIGFPE         9) SIGKILL       10) SIGUSR1
11) SIGSEGV        12) SIGUSR2       13) SIGPIPE       14) SIGALRM       15) SIGTERM
16) SIGSTKFLT      17) SIGCHLD       18) SIGCONT       19) SIGSTOP       20) SIGTSTP
21) SIGTTIN        22) SIGTTOU       23) SIGURG        24) SIGXCPU       25) SIGXFSZ
26) SIGVTALRM      27) SIGPROF       28) SIGWINCH      29) SIGIO         30) SIGPWR
31) SIGSYS         34) SIGRTMIN      35) SIGRTMIN+1    36) SIGRTMIN+2    37) SIGRTMIN+3
38) SIGRTMIN+4     39) SIGRTMIN+5    40) SIGRTMIN+6    41) SIGRTMIN+7    42) SIGRTMIN+8
43) SIGRTMIN+9     44) SIGRTMIN+10   45) SIGRTMIN+11   46) SIGRTMIN+12   47) SIGRTMIN+13
48) SIGRTMIN+14    49) SIGRTMIN+15   50) SIGRTMAX-14   51) SIGRTMAX-13   52) SIGRTMAX-12
53) SIGRTMAX-11    54) SIGRTMAX-10   55) SIGRTMAX-9    56) SIGRTMAX-8    57) SIGRTMAX-7
58) SIGRTMAX-6     59) SIGRTMAX-5    60) SIGRTMAX-4    61) SIGRTMAX-3    62) SIGRTMAX-2
63) SIGRTMAX-1     64) SIGRTMAX

[root@CentOS rdc]#

_SIGHUP_を使用してシステムを再起動します。

[root@CentOS]# pgrep systemd
1
464
500
643
15071

[root@CentOS]# kill -HUP 1

[root@CentOS]# pgrep systemd
1
464
500
643
15196
15197
15198

[root@CentOS]#
*pkill* を使用すると、管理者はプロセス名で_kill_シグナルを送信できます。
[root@CentOS]# pgrep ping
19450
[root@CentOS]# pkill -9 ping
[root@CentOS]# pgrep ping
[root@CentOS]#
*killall* はすべてのプロセスを強制終了します。 _killall_をrootとして使用すると、すべてのユーザーのすべてのプロセスが強制終了されるため、注意してください。
[root@CentOS]# killall chrome

無料のコマンド

*free* は、システムのメモリをすばやく確認するためによく使用される非常に単純なコマンドです。 使用済みの物理メモリとスワップメモリ​​の合計量が表示されます。
[root@CentOS]# free
             total       used      free      shared      buff/cache      available
Mem:        1879668     526284    699796     10304        653588          1141412
Swap:       3145724          0    3145724

[root@CentOS]#

素敵なコマンド

*nice* を使用すると、管理者はCPU使用率に関してプロセスのスケジューリング優先順位を設定できます。 良い点は、基本的にカーネルがプロセスまたはジョブのCPUタイムスライスをスケジュールする方法です。 デフォルトでは、プロセスにはCPUリソースへの平等なアクセス権が与えられていると想定されます。

まず、topを使用して、現在実行中のプロセスの良さを確認します。

PID   USER   PR   NI    VIRT    RES    SHR   S  %CPU  %MEM     TIME+    COMMAND
28    root   39   19       0      0      0   S  0.0   0.0    0:00.17    khugepaged
690   root   39   19   16808   1396   1164   S  0.0   0.1    0:00.01    alsactl]
9598  rdc    39   19  980596  21904  10284   S  0.0   1.2    0:00.27    tracker-extract
9599  rdc    39   19  469876   9608   6980   S  0.0   0.5    0:00.04    tracker-miner-a
9609  rdc    39   19  636528  13172   8044   S  0.0   0.7    0:00.12    tracker-miner-f
9611  rdc    39   19  469620   8984   6496   S  0.0   0.5    0:00.02    tracker-miner-u
27    root   25    5       0      0      0   S  0.0   0.0    0:00.00    ksmd
637   rtkit  21    1  164648   1276   1068   S  0.0   0.1    0:00.11    rtkit-daemon
1     root   20    0  128096   6712   3964   S  0.3   0.4    0:03.57    systemd
2     root   20    0       0      0      0   S  0.0   0.0    0:00.01    kthreadd
3     root   20    0       0      0      0   S  0.0   0.0    0:00.50    ksoftirqd/0
7     root   20    0       0      0      0   S  0.0   0.0    0:00.00    migration/0
8     root   20    0       0      0      0   S  0.0   0.0    0:00.00    rcu_bh
9     root   20    0       0      0      0   S  0.0   0.0    0:02.07    rcu_sched

_NI_で表される_NICE_列に注目します。 nicenessの範囲は、-20から正の19までの範囲です。 -20は最高の優先度を表します。

nohup nice --20 ping www.google.com &

renice

_renice_を使用すると、すでに実行されているプロセスの現在の優先順位を変更できます。

renice 17 -p 30727

上記のコマンドは、ping processコマンドの優先度を下げます。