Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't pgrep find this process?

Tags:

linux

grep

bash

ps

If I execute the following, which is just a long command that will wait forever

grep 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa abcd'

then pgrep -f can't find the process, if I search for abcd which is contained in the last segment of the long command.

ps auxww|grep abcd finds the process, but I can't use it in a script, as it also finds the grep process self.

If you remove just one a then pgrep -f abcd can find the process, but I have very long command with arguments, so I have run into this pgrep limitation.

Question

What is the correct way to check for such process based on the unique string abcd?

like image 703
Jasmine Lognnes Avatar asked Jul 20 '26 11:07

Jasmine Lognnes


2 Answers

Your edited command is found by either of these commands:

pgrep -f abcd

or even:

ps uxww | grep '[a]bcd'
like image 192
anubhava Avatar answered Jul 22 '26 00:07

anubhava


Let me try that...

$ grep 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa abcd'

Now in another terminal window:

$ pgrep grep
1842

Found it, or at least some grep process:

$ ps -f $(pgrep grep)
  UID   PID  PPID   C STIME   TTY           TIME CMD
  501  1842  1836   0  8:59AM ttys004    0:00.00 grep aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa abcd

Yup, that was the process found.

Let's try this:

$ pgrep -f 'abcd'
1842

Seems to work for me.

like image 24
David W. Avatar answered Jul 22 '26 01:07

David W.