LoginSignup
2
0

More than 3 years have passed since last update.

【Linux】psコマンドでCPU使用率を表示する、ヘッダーを表示する、grepのプロセスを表示しない方法

Posted at

概要

OS内部で現在実行されているプロセス一覧を表示するpsコマンドについて、
タイトルの3つの方法を調べたので、備忘の意味でまとめておく

psコマンドでCPU使用率を表示する

ps auxコマンドを使用する。%CPUの列の数値がCPU使用率を表示している

[oracle@user]$ ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  1.0  0.1  19400  1532 ?        Ss   18:37   0:00 /sbin/init
root         2  0.0  0.0      0     0 ?        S    18:37   0:00 [kthreadd]

CPU使用率の高い順にソートする場合は 
ps aux --sort -%cpu

[oracle@user]$ ps aux --sort -%cpu
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root      2248  1.2  2.4 113448 24728 tty1     Ss+  18:37   0:02 /usr/bin/Xorg :
oracle    2493  0.4  2.3 543792 23932 ?        S    18:38   0:00 nautilus
root         1  0.3  0.1  19400  1532 ?        Ss   18:37   0:00 /sbin/init

ヘッダーを表示する

psコマンドの結果をgrepでソートすると、ヘッダーが表示されなくなってしまう。
head -1 を追記することで、ヘッダーを表示する。

[oracle@user]$ ps aux | head -1 && ps aux | grep bash
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root      2029  0.0  0.0 108212   900 ?        S    18:37   0:00 /bin/bash /usr/sbin/ksmtuned
oracle    2796  0.0  0.1 108352  1936 pts/0    Ss   18:38   0:00 /bin/bash

grepのプロセスを表示しない

grepコマンドを使用したときに、[ ] 角括弧(かくかっこ)を利用して
grep自身がpsコマンドで出力されないようにする

#何もしないとgrep自身が表示される
[oracle@user]$ ps aux | grep bash
root      2029  0.0  0.0 108212   900 ?        S    18:37   0:00 /bin/bash /usr/sbin/ksmtuned
oracle    2796  0.0  0.1 108352  1936 pts/0    Ss   18:38   0:00 /bin/bash
oracle    2903  0.0  0.0 107500   892 pts/0    S+   18:48   0:00 grep bash

#角括弧をつけるとgrep自身は表示されなくなる
[oracle@user]$ ps aux | grep [b]ash
root      2029  0.0  0.0 108212   900 ?        S    18:37   0:00 /bin/bash /usr/sbin/ksmtuned
oracle    2796  0.0  0.1 108352  1936 pts/0    Ss   18:38   0:00 /bin/bash
2
0
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
2
0