pgrep

プロセスとシステム

書き方

pgrep [-f] <pattern>

pkill とまったく同じ規則で、一致するプロセスの PID だけを表示します。だから何かを殺す前の下見にちょうど向いています。

よく使うオプション

オプション意味
-fMatch the full command line, so arguments count too.
-aPrint the command line next to the PID (Linux).
-lPrint the process name next to the PID.
-xExact name match only.
-u <user>Restrict to one user.
-cPrint how many matched instead of the PIDs.
-n / -oNewest / oldest match only.

pgrep -af node

Every node process with its full command line.

pgrep -x sshd

The PID of sshd itself, not of anything merely mentioning ssh.

kill $(pgrep -f "celery worker")

Stop the processes the pattern found.

何が動いていて何が資源を食っているかを見ます。止める前に何なのかを確かめるのが順番です。

読み方

  • 角括弧 [ ] は省いてよい部分です。
  • 三点 … は複数並べられるという意味です。
  • オプションは大文字小文字を区別します — -r と -R が別物のコマンドもあります。

よくある質問

Q. pgrep は何をしますか。

pkill とまったく同じ規則で、一致するプロセスの PID だけを表示します。だから何かを殺す前の下見にちょうど向いています。

Q. どう打ちますか。

pgrep [-f] <pattern> — 角括弧は省いてよい部分です。

Q. よく使うオプションはいくつですか。

ここにまとめたのは7個です。全部は man pgrep にあります。このコマンドはプロセスとシステムの仲間です。

関連するコマンド

man ページ: man pgrep