KDOC 683: pgrepはプロセス名の先頭15文字しか照合しない
この文書のステータス
- 作成
- 2026-10-17 貴島
- レビュー
- 2026-10-17 貴島
概要
pgrep が照合するプロセス名は、 /proc/PID/stat にある先頭15文字だけである。それより長い名前の実行ファイルは、フルネームでは見つからない。 -f を付けると、 /proc/PID/cmdline のコマンドライン全体と照合する。
pgrep -V
MANWIDTH=80 man 1 pgrep | grep -A2 'The process name used for matching'
pgrep from procps-ng 4.0.4
The process name used for matching is limited to the 15 characters present
in the output of /proc/pid/stat. Use the -f option to match against the
complete command line, /proc/pid/cmdline. Threads may not have the same
15文字なのは、カーネルがプロセス名を終端のNULを含めて16バイト(TASK_COMM_LEN)に切り詰めて持つからである。切り詰めはバイト単位なので、UTF-8の日本語の名前は5文字になり、文字の途中で切れることもある。
MANWIDTH=80 man 5 proc_pid_comm | tail -1
MANWIDTH=80 man 5 proc_pid_comm | grep -A2 -E 'Strings +longer'
Linux man-pages 6.7 2023-08-15 proc_pid_comm(5)
/proc/self/task/tid/comm. Strings longer than TASK_COMM_LEN (16)
characters (including the terminating null byte) are silently trun‐
cated.
dir=$(mktemp -d)
cp /usr/bin/sleep "$dir/abcdefghijklmnopqrst"
"$dir/abcdefghijklmnopqrst" 10 & pid=$!
sleep 0.2
echo "comm: $(cat /proc/$pid/comm)"
for args in "-x abcdefghijklmnopqrst" "-x abcdefghijklmno" "-f abcdefghijklmnopqrst"; do
pgrep $args >/dev/null && r=一致 || r=不一致
echo "pgrep $args: $r"
done
kill $pid; rm -rf "$dir"
comm: abcdefghijklmno
pgrep -x abcdefghijklmnopqrst: 不一致
pgrep -x abcdefghijklmno: 一致
pgrep -f abcdefghijklmnopqrst: 一致
関連
- 前提: KDOC 125: /procのマニュアルを調べる。psやpgrepは /proc を情報源にしている