Linux シェルの pgrep コマンド (pgrep コマンド) を使用してプロセス番号を取得し、プロセス数をカウントする方法 (Linux プロセスのプロセス名を区別する方法を学びます)

問題の背景

以前の方法によると、スクリプトでは、スクリプト自体のプロセスに加えて、スクリプトと同じ名前のプロセス番号を取得します。

ps -ef | grep "${SCRIPT_NAME}" | grep -v "grep" | awk '{print $2}' | grep -v "$PID"

この方法には大きな問題があり、どういうわけか、grep プロセスを正常にフィルタリングすることができません (ここが少し複雑で、しばらく理解できませんでした。grep は子プロセスを開くことができますが、子プロセスを開くことはできないと言われています) grep プロセス。サブプロセスですが、スクリプトと同じプロセスを開いているため、問題が発生しています。具体的な参考資料: Linux シェル スクリプトがコマンドを実行するときのサブプロセス作成の問題 (バックグラウンド実行、パイプ、ブランチ、またはサブシェルなどの特定の状況で)など、スクリプトはコマンドを実行するために子プロセスが作成される場合があります) grep )

その後、pgrep コマンドに切り替えました。このコマンドを使用する利点は、grep コマンドを使用する必要がないことです。プロセス番号を直接検索し、追加のプロセス番号を取得しないことです。具体的な使用方法を見てみましょうpgrepコマンドの。

pgrepコマンド

pgrep コマンドは、プロセスの名前またはその他の属性に基づいて、一致するプロセス ID (PID) を検索して一覧表示するために使用されます。さまざまなオプションに基づいて柔軟なプロセスの検索とフィルタリングが可能になります。

ヘルプドキュメント

root@ubuntu:/ky/boot# pgrep --help

Usage:
 pgrep [options] <pattern>

Options:
 -d, --delimiter <string>  specify output delimiter
 -l, --list-name           list PID and process name
 -a, --list-full           list PID and full command line
 -v, --inverse             negates the matching
 -w, --lightweight         list all TID
 -c, --count               count of matching processes
 -f, --full                use full process name to match
 -g, --pgroup <PGID,...>   match listed process group IDs
 -G, --group <GID,...>     match real group IDs
 -i, --ignore-case         match case insensitively
 -n, --newest              select most recently started
 -o, --oldest              select least recently started
 -P, --parent <PPID,...>   match only child processes of the given parent
 -s, --session <SID,...>   match session IDs
 -t, --terminal <tty,...>  match by controlling terminal
 -u, --euid <ID,...>       match by effective IDs
 -U, --uid <ID,...>        match by real IDs
 -x, --exact               match exactly with the command name
 -F, --pidfile <file>      read PIDs from file
 -L, --logpidfile          fail if PID file is not locked
 -r, --runstates <state>   match runstates [D,S,Z,...]
 --ns <PID>                match the processes that belong to the same
                           namespace as <pid>
 --nslist <ns,...>         list which namespaces will be considered for
                           the --ns option.
                           Available namespaces: ipc, mnt, net, pid, user, uts

 -h, --help     display this help and exit
 -V, --version  output version information and exit

For more details see pgrep(1).

中国語訳:

root@ubuntu:/ky/boot# pgrep --help

用法:
 pgrep [选项] <模式>

选项:
 -d, --delimiter <字符串>  指定输出分隔符
 -l, --list-name           列出PID和进程名称
 -a, --list-full           列出PID和完整命令行
 -v, --inverse             反转匹配结果
 -w, --lightweight         列出所有TID
 -c, --count               统计匹配进程的数量
 -f, --full                使用完整进程名称进行匹配
 -g, --pgroup <PGID,...>   匹配指定的进程组ID
 -G, --group <GID,...>     匹配真实组ID
 -i, --ignore-case         不区分大小写进行匹配
 -n, --newest              选择最近启动的进程
 -o, --oldest              选择最早启动的进程
 -P, --parent <PPID,...>   仅匹配给定父进程的子进程
 -s, --session <SID,...>   匹配会话ID
 -t, --terminal <tty,...>  通过控制终端进行匹配
 -u, --euid <ID,...>       通过有效ID进行匹配
 -U, --uid <ID,...>        通过真实ID进行匹配
 -x, --exact               精确匹配命令名称
 -F, --pidfile <文件>      从文件中读取PID
 -L, --logpidfile          如果PID文件未锁定,则失败
 -r, --runstates <状态>    匹配运行状态 [D,S,Z,...]
 --ns <PID>                匹配与<pid>相同命名空间的进程
 --nslist <ns,...>         列出将用于--ns选项的命名空间
                           可用的命名空间:ipc, mnt, net, pid, user, uts

 -h, --help     显示此帮助信息并退出
 -V, --version  输出版本信息并退出

更多详细信息请参阅pgrep(1)

上記は、<pattern>一致するプロセス名またはその他の属性のパターンであることに注意してください。実際の状況に基づいて特定の値に置き換えることができます。

使用例

以下は、pgrepコマンドのさまざまなオプションの使用例です。

このコンテナでテストします。

注: まず、プロセスのプロセス名を区別する方法を学習する必要があります。
プロセス名はそのプロセスの実行ファイルです。プロセス名は 1 つだけです。プロセス名以外はすべてパスとパラメータです。
たとえば/bin/bash /usr/local/bin/entrypoint.sh、プロセス名は次のようになります。bash

root@5940438e0ee6:/build/libevent# echo "=====输出所有进程完整进程信息:"
=====输出所有进程完整进程信息:
root@5940438e0ee6:/build/libevent# ps -ef 
UID          PID    PPID  C STIME TTY          TIME CMD
root           1       0  0 Jun27 pts/0    00:00:00 /bin/bash /usr/local/bin/entrypoint.sh
root           7       1  0 Jun27 pts/0    00:02:56 ./kyai_rest
root          43       0  0 23:38 pts/1    00:00:00 /bin/bash
root          56      43  0 23:40 pts/1    00:00:00 ps -ef
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# echo "输出所有进程进程名:"
=====输出所有进程进程名:
root@5940438e0ee6:/build/libevent# ps -e -o comm=
bash
kyai_rest
bash
ps
root@5940438e0ee6:/build/libevent# 

1. 一致するプロセスの PID とプロセス名をリストします (-l) (デフォルトでは、プロセス名のサブセットのみが一致します。完全なプロセス名のサブセットを使用したい場合は、-f パラメータを追加してください) .以下同じ。)

pgrep -l <pattern>
pgrep --list-name <pattern>

例:

root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -l ky
7 kyai_rest
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -l ./
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -l ./ky
root@5940438e0ee6:/build/libevent#
root@5940438e0ee6:/build/libevent# pgrep -l -f ./ky
7 kyai_rest

2. PID と一致するプロセスの完全なコマンドラインをリストします (-a)

pgrep -a <pattern>
pgrep --list-full <pattern>

例:

root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -a ky
7 ./kyai_rest
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -a ./ky
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -a -f ./ky
7 ./kyai_rest
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -af ./ky
7 ./kyai_rest
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -a bash
1 /bin/bash /usr/local/bin/entrypoint.sh
43 /bin/bash
root@5940438e0ee6:/build/libevent# 

3. 一致するプロセスの数をカウントする (-c)

pgrep -c <pattern>
pgrep --count <pattern>

例:

root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -c ky
1
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -c bash
2
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -c ./ky
0
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -c -f ./ky
1
root@5940438e0ee6:/build/libevent# 

4. マッチングには完全なプロセス名 (パラメータを含む) のサブセットを使用します (-f) (パターンがプロセス コマンドとパラメータにまたがる場合は、二重引用符で囲む必要があります) ★★★★★

pgrep -f <pattern>
pgrep --full <pattern>

たとえば、フルネーム のプロセスがある場合はpython3 -u /ky/alg/kyai/nv/m/people/alg/main.py、次を使用します。

pgrep -f <pattern>

パターンはプロセスの完全名の任意の文字列サブセットであり、照合できます。

例1
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -f ky
7
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -f ./ky
7
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep ./ky
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep /usr/local/bin/entrypoint.sh
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -f /usr/local/bin/entrypoint.sh
1
root@5940438e0ee6:/build/libevent# 

例 2

ここに画像の説明を挿入します

5. 大文字と小文字を区別せずに一致させる (-i)

pgrep -i <pattern>
pgrep --ignore-case <pattern>

例:

root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -i KyAi
7
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -i entry
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -i -f eNtry
1
root@5940438e0ee6:/build/libevent# 

6. 最後に開始したプロセスを選択します (n)

pgrep -n <pattern>
pgrep --newest <pattern>

例:

root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -a bash
1 /bin/bash /usr/local/bin/entrypoint.sh
43 /bin/bash
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -a -n bash
43 /bin/bash
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -n bash
43
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# 

7. 最も早く開始されたプロセスを選択します (-o)

pgrep -o <pattern>
pgrep --oldest <pattern>

例:

root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -a bash
1 /bin/bash /usr/local/bin/entrypoint.sh
43 /bin/bash
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -a -o bash
1 /bin/bash /usr/local/bin/entrypoint.sh
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -o bash
1
root@5940438e0ee6:/build/libevent# 

8. 指定された親プロセスの子プロセスのみに一致します (-P)

pgrep -P <PPID> <pattern>
pgrep --parent <PPID> <pattern>

例:

たとえば、プロセス No. 0 には 2 つのサブプロセス No. 1 と No. 2 があります。

ここに画像の説明を挿入します

次のコマンドを使用してpgrep -P 0、プロセス 0 の子プロセスを検索します。

root@ubuntu:~# 
root@ubuntu:~# pgrep -P 0
1
2
root@ubuntu:~# 

子プロセスが見つかった場合はフィルターします。

root@ubuntu:~# 
root@ubuntu:~# pgrep -P 0 "system"
1
root@ubuntu:~# 
root@ubuntu:~# pgrep -P 0 "thread"
2
root@ubuntu:~# 
root@ubuntu:~# 

注: プロセス名ではない文字列セグメントを照合およびフィルターするために -f パラメーターを使用する場合は、二重引用符の使用と、二重引用符を使用した後でも注意が必要な問題に注意してください。

次のテストでは、両方ともエラーが報告されましたが、pgrep -f -P 0 --system問題がなかっただけです。pgrep -f -P 0 "--system"pgrep -f -P 0 " --system"

root@ubuntu:~# 
root@ubuntu:~# pgrep -f -P 0 --system
pgrep: unrecognized option '--system'

Usage:
 pgrep [options] <pattern>

Options:
 -d, --delimiter <string>  specify output delimiter
 -l, --list-name           list PID and process name
 -a, --list-full           list PID and full command line
 -v, --inverse             negates the matching
 -w, --lightweight         list all TID
 -c, --count               count of matching processes
 -f, --full                use full process name to match
 -g, --pgroup <PGID,...>   match listed process group IDs
 -G, --group <GID,...>     match real group IDs
 -i, --ignore-case         match case insensitively
 -n, --newest              select most recently started
 -o, --oldest              select least recently started
 -P, --parent <PPID,...>   match only child processes of the given parent
 -s, --session <SID,...>   match session IDs
 -t, --terminal <tty,...>  match by controlling terminal
 -u, --euid <ID,...>       match by effective IDs
 -U, --uid <ID,...>        match by real IDs
 -x, --exact               match exactly with the command name
 -F, --pidfile <file>      read PIDs from file
 -L, --logpidfile          fail if PID file is not locked
 -r, --runstates <state>   match runstates [D,S,Z,...]
 --ns <PID>                match the processes that belong to the same
                           namespace as <pid>
 --nslist <ns,...>         list which namespaces will be considered for
                           the --ns option.
                           Available namespaces: ipc, mnt, net, pid, user, uts

 -h, --help     display this help and exit
 -V, --version  output version information and exit

For more details see pgrep(1).
root@ubuntu:~# 
root@ubuntu:~# pgrep -f -P 0 "--system"
pgrep: unrecognized option '--system'

Usage:
 pgrep [options] <pattern>

Options:
 -d, --delimiter <string>  specify output delimiter
 -l, --list-name           list PID and process name
 -a, --list-full           list PID and full command line
 -v, --inverse             negates the matching
 -w, --lightweight         list all TID
 -c, --count               count of matching processes
 -f, --full                use full process name to match
 -g, --pgroup <PGID,...>   match listed process group IDs
 -G, --group <GID,...>     match real group IDs
 -i, --ignore-case         match case insensitively
 -n, --newest              select most recently started
 -o, --oldest              select least recently started
 -P, --parent <PPID,...>   match only child processes of the given parent
 -s, --session <SID,...>   match session IDs
 -t, --terminal <tty,...>  match by controlling terminal
 -u, --euid <ID,...>       match by effective IDs
 -U, --uid <ID,...>        match by real IDs
 -x, --exact               match exactly with the command name
 -F, --pidfile <file>      read PIDs from file
 -L, --logpidfile          fail if PID file is not locked
 -r, --runstates <state>   match runstates [D,S,Z,...]
 --ns <PID>                match the processes that belong to the same
                           namespace as <pid>
 --nslist <ns,...>         list which namespaces will be considered for
                           the --ns option.
                           Available namespaces: ipc, mnt, net, pid, user, uts

 -h, --help     display this help and exit
 -V, --version  output version information and exit

For more details see pgrep(1).
root@ubuntu:~# 
root@ubuntu:~# 
root@ubuntu:~# pgrep -f -P 0 " --system"
1
root@ubuntu:~# 
root@ubuntu:~# 

9. 制御端子(-t)による照合

使用

特定の端末名を指定して検索することにより、検索範囲を絞り込むことができます。マルチユーザー環境またはマルチ端末環境では、複数の端末が同じプロセスを同時に実行することがあります。端末名を指定すると、すべての端末ではなく、特定の端末でのみ一致するプロセスを検索できます。

これは、特定の端末でアクションを実行する必要がある場合、または特定の端末でプロセスを監視する必要がある場合に便利です。たとえば、特定の端末で実行されているアプリケーションのプロセスを検索したり、特定の端末でユーザーに関連付けられたプロセスを検索したりすることができます。

さらに、端末名の指定は、スクリプトまたは自動タスクでのプロセス管理に使用できます。特定の端末を指定すると、他の端末のプロセスに影響を与えることなく、その特定の端末上のプロセスに対してのみ操作が実行されるようにすることができます。

要約すると、検索する特定の端末名を指定すると、より正確で的を絞ったプロセスの検出と管理が可能になります。

注文
pgrep -t <tty> <pattern>
pgrep --terminal <tty> <pattern>
テスト手順

次の手順に従ってテストします。

  1. ターミナルを開きます。
  2. コマンドを実行してtty、現在の端末の名前を取得します。たとえば、端末名が の場合は/dev/tty1<tty>に置き換えますtty1
  3. 検索したいプロセス名またはキーワードに置き換えてpgrep -t <tty> <pattern>、コマンドを実行します。<pattern>たとえば、実行中のすべてのbashプロセスを検索したい場合は、 を実行しますpgrep -t tty1 bash
  4. コマンドが正常に実行されると、一致するプロセス ID が出力されます。
  5. pgrep --terminal <tty> <pattern>上記のコマンドと同じことを行うコマンドを実行してみることもできます。

pgrepこのコマンドは、プロセス リスト内で一致するプロセスを検索し、そのプロセス ID を出力するために使用されることに注意してください。<tty>は端末名、<pattern>は検索するプロセス名またはキーワードです。実際の状況に応じてこれらのパラメータを置き換える必要があります。

root@ubuntu:~# 
root@ubuntu:~# tty
/dev/pts/0
root@ubuntu:~# 
root@ubuntu:~# 
root@ubuntu:~# pgrep -t /dev/pts/0 bash
root@ubuntu:~# 
root@ubuntu:~# pgrep -t /dev/pts/0 sy
root@ubuntu:~# 
root@ubuntu:~# pgrep -t /dev/pts/0
root@ubuntu:~# 
root@ubuntu:~# pgrep -t "/dev/pts/0"
root@ubuntu:~# 
root@ubuntu:~# pgrep -t "/dev/pts/0" bash
root@ubuntu:~# 
root@ubuntu:~# 

恥ずかしいことに、この検索で​​は何も見つかりません

10. 有効な ID による照合 (-u)

使用

pgrep -u <ID> <pattern>およびpgrep --euid <ID> <pattern>コマンドは、指定されたユーザー ID または実効ユーザー ID で一致するプロセスを検索するために使用されます。

  • <ID>パラメータでは、ユーザー ID または有効なユーザー ID を指定します。ユーザーのユーザー名またはユーザー ID を置き換えることができます<ID>
  • <pattern>パラメータは、検索するプロセス名またはキーワードを指定するために使用されます。
注文
pgrep -u <ID> <pattern>
pgrep --euid <ID> <pattern>
テスト手順
  1. ターミナルを開きます。
  2. idコマンドを実行して、現在のユーザーのユーザー ID と実効ユーザー ID を取得します。たとえば、ユーザー ID が で1000、実効ユーザー ID が の場合1000<ID>に置き換えます1000
  3. 検索したいプロセス名またはキーワードに置き換えてpgrep -u <ID> <pattern>、コマンドを実行します。<pattern>たとえば、bash現在のユーザーの下で実行中のすべてのプロセスを検索したい場合は、 を実行できますpgrep -u 1000 bash
  4. コマンドが正常に実行されると、一致するプロセス ID が出力されます。
  5. pgrep --euid <ID> <pattern>上記のコマンドと同じことを行うコマンドを実行してみることもできます。

pgrepこのコマンドは、プロセス リスト内で一致するプロセスを検索し、そのプロセス ID を出力するために使用されることに注意してください。-uまたは--euidオプションは、ユーザー ID または有効なユーザー ID を指定するために使用されます。<ID>はユーザー ID または実効ユーザー ID、<pattern>は検索するプロセス名またはキーワードです。実際の状況に応じてこれらのパラメータを置き換える必要があります。

root@ubuntu:~# 
root@ubuntu:~# id
uid=0(root) gid=0(root) groups=0(root)
root@ubuntu:~# 
root@ubuntu:~# 
root@ubuntu:~# pgrep -u 0000 bash
3058
3955
23187
root@ubuntu:~# 
root@ubuntu:~# pgrep -u 0 bash
3058
3955
23187
root@ubuntu:~# 
root@ubuntu:~# pgrep -u 1000 bash
root@ubuntu:~# 
root@ubuntu:~# ps -ef | grep bash
root        1061       1  0 19:27 ?        00:00:00 /bin/bash /etc/systemd/nvmemwarning.sh
root        1260       1  0 19:28 ?        00:00:00 /bin/bash /ky/boot/kyai_nv_server.sh
root        1276       1  0 19:28 ?        00:00:00 /bin/bash /etc/systemd/nvgetty.sh
root        3058    2914  0 19:28 pts/0    00:00:00 /bin/bash /usr/local/bin/entrypoint.sh
root        3955    3915  0 19:28 pts/0    00:00:00 /bin/bash /usr/local/bin/entrypoint.sh
root       23187   23106  0 21:46 pts/0    00:00:00 -bash
root       36607   23187  0 23:39 pts/0    00:00:00 grep --color=auto bash
root@ubuntu:~# 
root@ubuntu:~# 

ここに画像の説明を挿入します

11. 実 ID による照合 (-U)

詳しくは実効IDと実IDを参照してください、まだよく分かりません。

注文
pgrep -U <ID> <pattern>
pgrep --uid <ID> <pattern>
root@ubuntu:~# 
root@ubuntu:~# pgrep -U 0000 bash
3058
3955
23187
root@ubuntu:~# 
root@ubuntu:~# 

12. コマンド名完全一致 (-x) ★★★

注文
pgrep -x <pattern>
pgrep --exact <pattern>
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# ps -ef 
UID          PID    PPID  C STIME TTY          TIME CMD
root           1       0  0 19:28 pts/0    00:00:00 /bin/bash /usr/local/bin/entrypoint.sh
root           8       1  0 19:28 pts/0    00:00:39 ./kyai_rest
root          41       0  0 23:45 pts/1    00:00:00 /bin/bash
root          54      41  0 23:46 pts/1    00:00:00 ps -ef
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -x kyai 
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -x kyai_rest
8
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -x -f kyai_rest
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -x -f ./kyai_rest
8
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -x bash          
1
41
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -x -f bash
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -x -f /bin/bash
41
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -x -f /bin/bash /usr/local/bin/entrypoint.sh
pgrep: only one pattern can be provided
Try `pgrep --help' for more information.
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# pgrep -x -f "/bin/bash /usr/local/bin/entrypoint.sh"
1
root@5940438e0ee6:/build/libevent# 
root@5940438e0ee6:/build/libevent# 

注: 完全一致とは、パラメータを含むすべての完全一致です。

ここに画像の説明を挿入します

おすすめ

転載: blog.csdn.net/Dontla/article/details/131407327