LoginSignup
5
5

More than 5 years have passed since last update.

特定のプロセスをpsコマンドで調べる時、1行目も一緒に表示させたい。

Last updated at Posted at 2014-01-19

どのカラムがどんな意味だったか覚えてらんないよね。

$ ps -ef |awk 'NR==1||/[p]s/'
UID        PID  PPID  C STIME TTY          TIME CMD
root        79     2  0 Jan13 ?        00:00:00 [kpsmoused]
mutz0623 25544  3936  0 20:56 pts/0    00:00:00 ps -ef

上記ではawkの文法を若干省略している。
省略せずに記述するなら

$ ps -ef |awk 'NR==1 || $0~/[p]s/{print $0}'
UID        PID  PPID  C STIME TTY          TIME CMD
root        79     2  0 Jan13 ?        00:00:00 [kpsmoused]
mutz0623 28011  3936  0 21:14 pts/0    00:00:00 ps -ef

あるいは

$ ps -ef |awk 'NR==1{print $0} $0~/[p]s/{print $0}'
UID        PID  PPID  C STIME TTY          TIME CMD
root        79     2  0 Jan13 ?        00:00:00 [kpsmoused]
mutz0623 25460  3936  0 20:55 pts/0    00:00:00 ps -ef

といったところでしょうか。
/[p]s/としているのは↓の様になるのを防ぐため

$ ps aux |awk 'NR==1||/ps/'
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root        79  0.0  0.0      0     0 ?        S    Jan13   0:00 [kpsmoused]
mutz0623 26495  0.0  0.0 110228  1160 pts/0    R+   21:03   0:00 ps aux
mutz0623 26496  0.0  0.0 105944  1068 pts/0    S+   21:03   0:00 awk NR==1||/ps/

(2017年10月18日追記)

psコマンド自体が特定のプロセスをプロセス名やユーザーを指定して絞り込む機能を持っているので、それを使う方がスマートですね。

$ ps -f -C httpd
UID        PID  PPID  C STIME TTY          TIME CMD
apache    4266  5357  0 18:06 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    5187  5357  0 17:31 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
root      5357     1  0 10月15 ?      00:00:15 /usr/sbin/httpd -DFOREGROUND
apache    6851  5357  0 17:33 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    6852  5357  0 17:33 ?        00:00:01 /usr/sbin/httpd -DFOREGROUND
apache   10212  5357  0 15:50 ?        00:00:03 /usr/sbin/httpd -DFOREGROUND
apache   11586  5357  0 18:17 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   11587  5357  0 18:17 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   15313  5357  0 16:58 ?        00:00:01 /usr/sbin/httpd -DFOREGROUND
apache   28507  5357  0 16:05 ?        00:00:02 /usr/sbin/httpd -DFOREGROUND
apache   31295  5357  0 15:33 ?        00:00:04 /usr/sbin/httpd -DFOREGROUND
apache   32637  5357  0 18:00 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
$ ps -f --user apache
UID        PID  PPID  C STIME TTY          TIME CMD
apache    4266  5357  0 18:06 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    5187  5357  0 17:31 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    6851  5357  0 17:33 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache    6852  5357  0 17:33 ?        00:00:01 /usr/sbin/httpd -DFOREGROUND
apache   10212  5357  0 15:50 ?        00:00:03 /usr/sbin/httpd -DFOREGROUND
apache   11586  5357  0 18:17 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   11587  5357  0 18:17 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache   15313  5357  0 16:58 ?        00:00:01 /usr/sbin/httpd -DFOREGROUND
apache   28507  5357  0 16:05 ?        00:00:02 /usr/sbin/httpd -DFOREGROUND
apache   31295  5357  0 15:33 ?        00:00:04 /usr/sbin/httpd -DFOREGROUND
apache   32637  5357  0 18:00 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND

awkのtipsでもなんでもなくなりました。

5
5
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
5
5