ps | grep
で特定のプロセスを検索するときに、grep
自身のプロセスを除外する方法をメモしておきます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 検索結果の2行目を除外したい | |
$ ps aux | grep jar | |
donchan922 58750 0.1 3.5 7931104 295728 s001 S+ 7:08PM 0:15.23 /usr/bin/java -jar build/libs/demo-0.0.1-SNAPSHOT.jar | |
donchan922 58927 0.0 0.0 4268040 784 s000 S+ 7:11PM 0:00.00 grep jar |
grep -v grep
を使う
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ps aux | grep -v grep | grep java | |
donchan922 58750 0.1 3.5 7931104 296088 s001 S+ 7:08PM 0:17.62 /usr/bin/java -jar build/libs/demo-0.0.1-SNAPSHOT.jar |
grep -v XXX
とすると、XXX
を含む行を除外します。上記コードでは、grep -v grep
でgrep
自身のプロセスを除外できます。
grep
コマンドだけで実現できるのがいいところですね。
pgrep
を使う
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# プロセス名に対してgrepする。プロセス番号(PID)を表示する | |
$ pgrep java | |
58750 | |
# -f: プロセス名だけでなく、コマンドライン全体に対してgrepする | |
# -l: プロセス番号(PID)だけでなく、コマンドライン全体を表示する | |
$ pgrep -f -l java | |
58750 /usr/bin/java -jar build/libs/demo-0.0.1-SNAPSHOT.jar |
pgrep
は特定のプロセスを検索するためのコマンドです。シンプルに書けていいですね。
まとめ
ps | grep
の結果からgrep
自身を除外する2つの方法でした。