9

pgrep returns nothing if the pattern length is longer than 15 characters. I can't find anything in the man page about maximum pattern length.

Is this a bug?

for purposes of exposition I created a process called 12345678901234567890

$ pgrep 12345678901234
5367
$ pgrep 123456789012345
5367
$ pgrep 1234567890123456
$ 
4

I think it's not a limit on the pattern length in pgrep itself, but a limit on the length of the comm (command) field in the /proc/[pid]/stat files (which is where the information in ps comes from).

According to man proc, the definitions for /proc/[pid]/stat come from /usr/src/linux/fs/proc/array.c so you would need to look there for a definitive answer.

6

According to the pgrep man page (emphasis mine):

Notes

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.

The reason is that process names are limited to 16 bytes including the NULL termination byte. From the prctl manpage:

PR_SET_NAME (since Linux 2.6.9)

Set the name of the calling thread, using the value in the location pointed to by (char *) arg2. The name can be up to 16 bytes long, including the terminating null byte. (If the length of the string, including the terminating null byte, exceeds 16 bytes, the string is silently truncated.)

See What is the maximum allowed limit on the length of a process name?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.