7

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?

Jasmine Lognnes
  • 6,597
  • 9
  • 38
  • 58
  • When data is sent to the `mbuffer` process it will exit when the data stream ends. – Jasmine Lognnes Dec 29 '14 at 05:17
  • 1
    So how do you know process is still running? Did you check `top` command? btw why downvote to this question? +1 – anubhava Dec 29 '14 at 05:21
  • 1
    The purpose of the long command is to receive data over port 8023. In my script do I have this long receiver command and also the sending command. Even though the sending command exit, it can still take a little while before the receiving commands exits, so that is why I want to check for `abcd`. If I find it, then I will wait a second, check again, and only continue when I don't see `abcd` in the process list. – Jasmine Lognnes Dec 29 '14 at 05:31
  • So even `top` is not showing this process in the list? – anubhava Dec 29 '14 at 05:35
  • I can't see it, but then again it doesn't take any cpu when no data is sent to it. I have updated the OP, so now it can be reproduced on my Linux. – Jasmine Lognnes Dec 29 '14 at 05:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/67829/discussion-between-jasmine-lognnes-and-anubhava). – Jasmine Lognnes Dec 29 '14 at 05:50
  • 1
    `pgrep` is in general the solution you desire. However, It's easier sometimes to get the result in `ps auxww`'s format, rather than just PID (as returned by pgrep). Use `ps auxww|grep [a]bcd` instead of `ps auxww|grep abcd` – anishsane Dec 29 '14 at 05:51

2 Answers2

12

Your edited command is found by either of these commands:

pgrep -f abcd

or even:

ps uxww | grep '[a]bcd'
anubhava
  • 761,203
  • 64
  • 569
  • 643
2

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.

David W.
  • 105,218
  • 39
  • 216
  • 337