GNU/Linux >> Linux の 問題 >  >> Linux

メモリとCPUの使用量で実行中のプロセスのトップ10を探す

Linuxはオープンソースで非常に人気のあるオペレーティングシステムであり、インターネット上にサーバーとアプリケーションを展開するための推奨オプションです。コマンドラインインターフェイスと組み込みツールにより、タスクをすばやく実行して時間を大幅に節約できるため、非常に人気があります。

メモリとCPUの使用率が高いために、アプリケーションやWebサイトが非常に遅いか応答しない場合があります。この場合、psおよびtopコマンドを使用して、システム上のすべてのリソースを消費しているプロセスを特定できます。

この投稿では、Linuxでトップメモリ​​とCPUリソースを消費するプロセスを見つける方法を説明します。

前提条件

  • Atlantic.NetクラウドプラットフォームでLinuxを実行しているサーバー
  • サーバーで構成されているrootパスワード

Atlantic.Netクラウドサーバーを作成する

まず、Atlantic.Netクラウドサーバーにログインします。少なくとも1GBのRAMを搭載したLinuxオペレーティングシステムを選択して、新しいサーバーを作成します。 SSH経由でクラウドサーバーに接続し、ページの上部で強調表示されているクレデンシャルを使用してログインします。

psコマンドを使用してメモリとCPU使用率で上位のプロセスを検索する

ps は、さまざまな形式で出力を表示するのに役立つ多くのオプションを備えたLinuxコマンドラインユーティリティです。

Linuxで実行中のすべてのプロセスを一覧表示するには、次のコマンドを実行します。

ps aux

これにより、実行中のすべてのプロセスの概要が表示されます。

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.1  0.1 160716  9904 ?        Ss   16:58   0:06 /sbin/init splash
root         2  0.0  0.0      0     0 ?        S    16:58   0:00 [kthreadd]
root         3  0.0  0.0      0     0 ?        I<   16:58   0:00 [rcu_gp]
root         4  0.0  0.0      0     0 ?        I<   16:58   0:00 [rcu_par_gp]
root         6  0.0  0.0      0     0 ?        I<   16:58   0:00 [kworker/0:0H-kb]
root         9  0.0  0.0      0     0 ?        I<   16:58   0:00 [mm_percpu_wq]
root        10  0.0  0.0      0     0 ?        S    16:58   0:00 [ksoftirqd/0]
root        11  0.1  0.0      0     0 ?        I    16:58   0:05 [rcu_sched]
root        12  0.0  0.0      0     0 ?        S    16:58   0:00 [migration/0]

psコマンドは–sortで使用できます メモリとCPU使用率で出力を並べ替える引数。

sort引数を使用するための構文は次のとおりです。

ps aux --sort

CPU使用率別に実行中のプロセスを見つけるには、次のコマンドを実行します。

ps aux --sort -%cpu

出力:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
vyom      8115  7.8  3.0 25770108 233784 tty2  Sl+  17:58   1:37 /opt/google/chrome/chrome --type=renderer --enable-crashpad --crashpad-handler-pid=2815 --enable-crash-reporter=373d0de2-e0c8-419c-b983-084c773fcd79, --display-capture-permissions-policy-allowed --change-stack-guard-on-fork=enable --lang=en-GB --num-raster-threads=1 --renderer-client-id=82 --launch-time-ticks=3564377766 --shared-files=v8_context_snapshot_data:100 --field-trial-handle=0,i,7343938639469663677,16234295293987540603,131072 --enable-features=PasswordImport
vyom      8164  6.2  3.0 25705000 233456 tty2  Sl+  17:58   1:16 /opt/google/chrome/chrome --type=renderer --enable-crashpad --crashpad-handler-pid=2815 --enable-crash-reporter=373d0de2-e0c8-419c-b983-084c773fcd79, --display-capture-permissions-policy-allowed --change-stack-guard-on-fork=enable --lang=en-GB --num-raster-threads=1 --renderer-client-id=85 --launch-time-ticks=3576904510 --shared-files=v8_context_snapshot_data:100 --field-trial-handle=0,i,7343938639469663677,16234295293987540603,131072 --enable-features=PasswordImport
vyom      2806  4.4  4.5 17565904 343660 tty2  SLl+ 17:01   3:26 /opt/google/chrome/chrome --enable-crashpad
vyom      2314  4.3  2.9 3472696 222248 tty2   Rl+  17:00   3:22 /usr/bin/gnome-shell

メモリ使用量で上位のプロセスを見つけるには、次のコマンドを実行します。

ps aux --sort -%mem

出力:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
vyom      2806  4.4  4.5 17562832 343688 tty2  SLl+ 17:01   3:26 /opt/google/chrome/chrome --enable-crashpad
vyom      8115  7.8  3.0 25770108 232204 tty2  Sl+  17:58   1:38 /opt/google/chrome/chrome --type=renderer --enable-crashpad --crashpad-handler-pid=2815 --enable-crash-reporter=373d0de2-e0c8-419c-b983-084c773fcd79, --display-capture-permissions-policy-allowed --change-stack-guard-on-fork=enable --lang=en-GB --num-raster-threads=1 --renderer-client-id=82 --launch-time-ticks=3564377766 --shared-files=v8_context_snapshot_data:100 --field-trial-handle=0,i,7343938639469663677,16234295293987540603,131072 --enable-features=PasswordImport
vyom      8164  6.1  3.0 25705000 230116 tty2  Sl+  17:58   1:16 /opt/google/chrome/chrome --type=renderer --enable-crashpad --crashpad-handler-pid=2815 --enable-crash-reporter=373d0de2-e0c8-419c-b983-084c773fcd79, --display-capture-permissions-policy-allowed --change-stack-guard-on-fork=enable --lang=en-GB --num-raster-threads=1 --renderer-client-id=85 --launch-time-ticks=3576904510 --shared-files=v8_context_snapshot_data:100 --field-trial-handle=0,i,7343938639469663677,16234295293987540603,131072 --enable-features=PasswordImport

メモリを消費している上位10個のプロセスのみを表示する場合は、次のコマンドを実行します。

ps aux --sort -%mem | head -10

コマンドのフルパスではなくコマンド名のみを表示する場合は、次のコマンドを実行します。

ps -eo pid,ppid,cmd,comm,%mem,%cpu --sort=-%mem | head -10

出力:

  PID  PPID CMD                         COMMAND         %MEM %CPU
 2806     1 /opt/google/chrome/chrome - chrome           4.5  4.3
 8164  2828 /opt/google/chrome/chrome - chrome           3.2  6.2
 8115  2828 /opt/google/chrome/chrome - chrome           3.0  7.8
 2314  2176 /usr/bin/gnome-shell        gnome-shell      2.9  4.4
 2996  2828 /opt/google/chrome/chrome - chrome           2.4  0.5
 8074  2828 /opt/google/chrome/chrome - chrome           2.4  0.8
 7520  2828 /opt/google/chrome/chrome - chrome           2.4  0.3
 8175  2828 /opt/google/chrome/chrome - chrome           2.2  0.3
 2858  2823 /opt/google/chrome/chrome - chrome           2.2  3.8

topコマンドを使用してメモリとCPU使用率で上位のプロセスを検索

topは、Linuxで実行中のすべてのプロセスを表示するために使用できるもう1つの組み込みのLinuxコマンドラインユーティリティです。 topコマンドでさまざまなオプションを使用して、要件に基づいて出力をフィルタリングできます。

topコマンドは-oで使用できます 上位のメモリ消費プロセスを示すフラグ:

top -o %MEM

出力:

Tasks: 329 total,   1 running, 281 sleeping,   0 stopped,   0 zombie
%Cpu(s):  1.5 us,  0.7 sy,  0.0 ni, 94.6 id,  3.2 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  7580260 total,  2602168 free,  2668376 used,  2309716 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.  4486960 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                   
 2806 vyom      20   0 16.753g 343596 183124 S   0.0  4.5   3:37.39 chrome                                                                    
 8164 vyom      20   0 24.516g 241968 103164 S   0.0  3.2   1:44.22 chrome                                                                    
 8115 vyom      20   0 24.570g 237756 113464 S   0.0  3.1   2:04.79 chrome                                                                    
 2314 vyom      20   0 3472696 222248  97556 S   1.3  2.9   3:49.13 gnome-shell                                                               
 8074 vyom      20   0 24.508g 187804 110344 S   0.0  2.5   0:11.59 chrome                                                                    
 7520 vyom      20   0 24.563g 185760 104852 S   0.0  2.5   0:06.98 chrome                                                                    
 2996 vyom      20   0 24.503g 185316  85720 S   0.0  2.4   0:27.90 chrome                                                                    
 8175 vyom      20   0 24.518g 171224 100040 S   0.0  2.3   0:04.96 chrome                                                                    

メモリを消費している上位10個のプロセスのみを表示する場合は、次のコマンドを実行します。

top -o %MEM | head -n 16

出力:

top - 18:31:11 up  1:32,  1 user,  load average: 0.32, 0.41, 0.65
Tasks: 330 total,   1 running, 282 sleeping,   0 stopped,   0 zombie
%Cpu(s): 14.3 us,  3.5 sy,  0.1 ni, 78.8 id,  3.1 wa,  0.0 hi,  0.1 si,  0.0 st
KiB Mem :  7580260 total,  2623576 free,  2655868 used,  2300816 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.  4508812 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                   
 2806 vyom      20   0 16.749g 343920 183192 S   0.0  4.5   3:39.02 chrome                                                                    
 8164 vyom      20   0 24.513g 232772 103276 S   0.0  3.1   1:45.33 chrome                                                                    
 8115 vyom      20   0 24.557g 230336 111896 S   0.0  3.0   2:05.62 chrome                                                                    
 2314 vyom      20   0 3472696 222880  97560 S   0.0  2.9   3:59.60 gnome-shell                                                               
 8074 vyom      20   0 24.508g 187740 110344 S   0.0  2.5   0:11.61 chrome                                                                    
 7520 vyom      20   0 24.563g 185724 104852 S   0.0  2.5   0:06.99 chrome                                                                    
 2996 vyom      20   0 24.503g 185012  85720 S   0.0  2.4   0:27.98 chrome                                                                    
 8175 vyom      20   0 24.518g 171224 100040 S   0.0  2.3   0:04.97 chrome                                                                    
 2735 vyom      20   0 37.371g 168668 119056 S   0.0  2.2   0:05.57 skypeforlinux                                                             

CPUを消費している上位10個のプロセスのみを表示する場合は、次のコマンドを実行します。

top -o %CPU | head -n 16

出力:

top - 18:32:05 up  1:33,  1 user,  load average: 0.48, 0.43, 0.64
Tasks: 330 total,   1 running, 282 sleeping,   0 stopped,   0 zombie
%Cpu(s): 14.2 us,  3.5 sy,  0.1 ni, 78.9 id,  3.1 wa,  0.0 hi,  0.1 si,  0.0 st
KiB Mem :  7580260 total,  2621204 free,  2662180 used,  2296876 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.  4506588 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                   
 2314 vyom      20   0 3472696 223296  97560 S  11.8  2.9   4:03.68 gnome-shell                                                               
 2161 vyom      20   0  998276  86120  55136 S   5.9  1.1   2:30.95 Xorg                                                                      
 8822 vyom      20   0   44368   4188   3364 R   5.9  0.1   0:00.02 top                                                                       
    1 root      20   0  160716   9904   6644 S   0.0  0.1   0:06.65 systemd                                                                   
    2 root      20   0       0      0      0 S   0.0  0.0   0:00.00 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                                                                
    6 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 kworker/0:0H-kb                                                           
    9 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 mm_percpu_wq                                                              

結論

この投稿では、LinuxでCPUとメモリを最も消費するプロセスを見つける方法について説明しました。これらのコマンドを使用して、Linuxのパフォーマンス関連の問題をトラブルシューティングできます。 Atlantic.NetのVPSホスティングでお試しください!


Linux
  1. シェルスクリプトでCPU使用率を取得しますか?

  2. Linuxで単一プロセスのCPU使用率とメモリ使用率を取得しますか?

  3. Linuxでユーザーのメモリ使用量を見つける方法

  1. バッチモードで「top」を使用してメモリ使用量で上位15のプロセスを検索

  2. 非常に高い CPU 使用率で実行されている Jenkins

  3. メモリ使用量によるプロセスの並べ替え

  1. Linuxで最も高いメモリとCPU使用率で上位のプロセスを検索する

  2. メモリとCPU使用率で上位のプロセスを見つける方法

  3. 実際のメモリ使用量でソートされた上位プロセスを確認するには?