LoginSignup
21
17

More than 3 years have passed since last update.

psコマンドまとめ

Posted at

できること

プロセス単位でのCPU,メモリ,状態などの確認

実行結果

使い方: ps [オプション]

オプション 説明
a 端末を持つ全てのプロセスを表示(ターミナルから実行したものなど)
e 環境変数名を表示
x 端末のないプロセスも表示
u 実行ユーザ名の表示
r 実行中のプロセスのみ表示
-e,-A 全てのプロセスを表示
-f 完全なフォーマットで表示
-L スレッドの表示(LWPとNLWPカラムをつけてスレッドを表示する。)

LWP(light weight process):軽量プロセス(=スレッド)
NLWP(Number of LWP):プロセスの持つスレッド数

端末を持つプロセスの表示

↓のように、TTY(制御端末名)の項目に値を持つプロセスのみ表示される

# ps a
  PID TTY      STAT   TIME COMMAND
 2149 tty1     Ss+    0:00 /sbin/agetty --noclear tty1 linux
 3254 pts/0    Ss     0:00 -bash
 3634 pts/0    S      0:00 sudo su -
 3635 pts/0    S      0:00 su -

環境変数の表示

↓のようにCOMMANDの項目に環境変数が追加される

# ps e
  PID TTY      STAT   TIME COMMAND
 2149 tty1     Ss+    0:00 /sbin/agetty --noclear tty1 linux LANG= PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin TERM=linux LANGUAGE= LC_CTYPE= LC_NUMERIC= LC_TIME= LC
 3634 pts/0    S      0:00 sudo su - XDG_SESSION_ID=1 HOSTNAME=webapp1 TERM=xterm-256color SHELL=/bin/bash HISTSIZE=1000 SSH_CLIENT=10.0.2.2 55579 22 SSH_TTY=/dev/pts/0 USER=va
 3635 pts/0    S      0:00 su - HOSTNAME=webapp1 TERM=xterm-256color HISTSIZE=1000 LS_COLORS=rs=0:di=38;5;27:ln=38;5;51:mh=44;38;5;15:pi=40;38;5;11:so=38;5;13:do=38;5;5:bd=48;5
 3636 pts/0    S      0:00 -bash TERM=xterm-256color HOME=/root SHELL=/bin/bash USER=root LOGNAME=root PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin XDG_SES

全プロセスの確認

# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.4 125460  2252 ?        Ss   09:57   0:01 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
root         2  0.0  0.0      0     0 ?        S    09:57   0:00 [kthreadd]
root         3  0.0  0.0      0     0 ?        S    09:57   0:03 [ksoftirqd/0]
root         5  0.0  0.0      0     0 ?        S<   09:57   0:00 [kworker/0:0H]
root         7  0.0  0.0      0     0 ?        S    09:57   0:00 [migration/0]
項目 説明
USER 実行ユーザ名
PID プロセスID
%CPU プロセスのCPU使用率
%MEM プロセスのメモリ使用率
VSZ プロセスが確保する仮想メモリサイズ
RSS プロセスが確保する物理メモリサイズ
TTY 制御端末名
STAT プロセスの状態
START 実行開始時刻
TIME 消費したCPU時間
COMMAND 実行コマンド

(補足)プロセスの状態「S」について

状態 説明 備考
S スリープ(割り込み可,イベント完了待ち) Interruptible sleep (waiting for an event to complete)
T 停止中 Stopped, either by a job control signal or because it is being traced.
D スリープ(割り込み不可,IO完了待ち) Uninterruptible sleep (usually IO)
R 実行中or実行可能 Running or runnable (on run queue)
Z ゾンビ状態 Zombie state: Process terminated and parent not waiting
W スワップアウト状態 paging (not valid since the 2.6.xx kernel)

スレッドの確認

↓の例ではmysqlの1プロセス(プロセスID:3113)が29個のスレッドを持つのがわかる

# ps -efL
UID        PID  PPID   LWP  C NLWP STIME TTY          TIME CMD
(一部省略)
mysql     3113  2811  3113  0   29 09:58 ?        00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log
mysql     3113  2811  3149  0   29 09:58 ?        00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log
mysql     3113  2811  3168  0   29 09:58 ?        00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log
mysql     3113  2811  3169  0   29 09:58 ?        00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log
mysql     3113  2811  3170  0   29 09:58 ?        00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log
m
(以下省略)
項目 説明
PPID 親プロセスのプロセスID
LWP スレッドのID
C プロセッサ使用率
NLWP プロセスにおけるスレッド数
21
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
17