LoginSignup
41
42

More than 5 years have passed since last update.

意外と知られてないかもしれないlinuxコマンドのオプション

Last updated at Posted at 2016-02-18

よく使うコマンド。

psで特定のプロセスだけ見る

rubyのプロセスだけ見たいときps auxww | grep ruby が美しくないと感じる方向けに-Cオプションが便利。

ps -Cruby ufw
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
webapp   19078  0.0  2.6 113468 26572 ?        Ssl  22:08   0:00 puma 2.10.2 (unix:///var/run/puma/my_app.sock)
webapp   19084  0.3 18.3 845408 186564 ?       Sl   22:08   0:16  \_ puma: cluster worker 0: 19078
healthd   2410  0.0  2.6 686832 27316 ?        Ssl   2015  50:45 puma 2.11.1 (tcp://127.0.0.1:22221) [healthd]

,区切りで複数のコマンドを指定できるので、nginxとrubyが見たい場合。

ps -Cruby,nginx ufw
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
webapp   19078  0.0  2.6 113468 26572 ?        Ssl  22:08   0:00 puma 2.10.2 (unix:///var/run/puma/my_app.sock)
webapp   19084  0.3 18.3 845408 186564 ?       Sl   22:08   0:16  \_ puma: cluster worker 0: 19078
healthd   2410  0.0  2.6 686832 27320 ?        Ssl   2015  50:45 puma 2.11.1 (tcp://127.0.0.1:22221) [healthd]
root      2672  0.0  0.8 112852  8816 ?        Ss    2015   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx    18721  0.0  0.9 113016  9644 ?        S    22:07   0:00  \_ nginx: worker process

psで特定のユーザのプロセスだけ見る

-Uオプションを使う

ps -Uwebapp ufw
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
webapp    6270  0.0  1.9 113428 20328 ?        Ssl  Mar01   0:24 puma 2.10.2 (unix:///var/run/puma/my_app.sock)
webapp    6291  0.2 16.9 847548 172956 ?       Sl   Mar01  45:58  \_ puma: cluster worker 0: 6270

-U,区切りで複数のユーザを指定できる

ps -Uwebapp,ec2-user ufw
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
ec2-user 32296  0.0  0.3 115656  3780 ?        S    12:05   0:00 sshd: ec2-user@pts/0
ec2-user 32297  0.0  0.3 115564  3544 pts/0    Ss   12:05   0:00  \_ -bash
ec2-user 32375  0.0  0.2 117152  2472 pts/0    R+   12:07   0:00      \_ ps -Uwebapp,ec2-user ufw
webapp    6270  0.0  1.9 113428 20332 ?        Ssl  Mar01   0:24 puma 2.10.2 (unix:///var/run/puma/my_app.sock)
webapp    6291  0.2 16.9 847548 172960 ?       Sl   Mar01  45:59  \_ puma: cluster worker 0: 6270

リッスンしているポートを掴んでいるコマンドを表示

netstatはオワコンらしいがよく使っている。
-pオプション(root権限必要)を付けるとPIDとコマンド名を表示してくれるので便利。

sudo netstat -lnatp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      2230/sshd
tcp        0      0 127.0.0.1:8600              0.0.0.0:*                   LISTEN      19044/consul
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      2270/sendmail
tcp        0      0 0.0.0.0:24224               0.0.0.0:*                   LISTEN      19013/ruby
tcp        0      0 127.0.0.1:22221             0.0.0.0:*                   LISTEN      2410/puma 2.11.1 (t
tcp        0      0 0.0.0.0:40781               0.0.0.0:*                   LISTEN      2084/rpc.statd
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      2067/rpcbind
tcp        0      0 127.0.0.1:8400              0.0.0.0:*                   LISTEN      19044/consul
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      2672/nginx
tcp        0      0 127.0.0.1:8500              0.0.0.0:*                   LISTEN      19044/consul

dmesgの日付を人が読める形で表示

-Tオプションを付ける

dmesg -T
...
[Tue Nov 24 23:22:27 2015] evbug: Connected device: input0 (AT Translated Set 2 keyboard at isa0060/serio0/input0)
[Tue Nov 24 23:22:27 2015] evbug: Connected device: input2 (Power Button at LNXPWRBN/button/input0)
[Tue Nov 24 23:22:27 2015] evbug: Connected device: input3 (Sleep Button at LNXSLPBN/button/input0)
[Tue Nov 24 23:22:28 2015] input: ImExPS/2 Generic Explorer Mouse as /devices/platform/i8042/serio1/input/input4
[Tue Nov 24 23:22:28 2015] evbug: Connected device: input4 (ImExPS/2 Generic Explorer Mouse at isa0060/serio1/input0)
[Tue Nov 24 23:22:31 2015] EXT4-fs (xvda1): re-mounted. Opts: (null)
[Tue Nov 24 23:22:31 2015] NET: Registered protocol family 10

ipの出力を一行にする

出力が1行にまとまっている方が良かったりする場合、-oオプションが便利。

ip -f inet -o addr show
1: lo    inet 127.0.0.1/8 scope host lo\       valid_lft forever preferred_lft forever
2: eth0    inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0\       valid_lft forever preferred_lft forever
3: eth1    inet 192.168.10.3/24 brd 192.168.10.255 scope global eth1\       valid_lft forever preferred_lft forever

topの結果をパイプなどに流したい場合

-bオプションを付ける

top -b -n 1 | head
top - 18:19:13 up 11 days, 11:36,  1 user,  load average: 0.00, 0.01, 0.05
Tasks: 129 total,   1 running, 128 sleeping,   0 stopped,   0 zombie
%Cpu(s):  2.1 us,  0.8 sy,  0.0 ni, 97.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem:    501736 total,   463892 used,    37844 free,     3620 buffers
KiB Swap:  4194300 total,   498476 used,  3695824 free.    34032 cached Mem

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
  935 app       20   0  900316 166816   1012 S  6.3 33.2   7:22.24 ruby
    1 root      20   0   33632    244    220 S  0.0  0.0   0:01.41 init
    2 root      20   0       0      0      0 S  0.0  0.0   0:00.04 kthreadd
41
42
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
41
42