2

I'm using pgrep to find running processes having some words

> pgrep -f "otp"
2345
2343

More than one process is identified. There is another word in the command line that would help me zero in to the exact process. So, I want to search for two terms and both should be there. Unfortunately I'm only seeing examples and help for OR combinations like this:

> pgrep -f "otp|place1"
2345
2343
3632

That does not work because it's an OR condition. I need an AND condition in my search pattern.

pgrep reference: https://linux.die.net/man/1/pgrep

pattern: Specifies an Extended Regular Expression for matching against the process names or command lines.

How do I use pgrep to search for two words in AND combination instead of OR?

Nikhil VJ
  • 141
  • 1
  • 6

2 Answers2

2

Found one way, with the condition being that they should be in order: separate the words by .*

> pgrep -f "otp.*place1"
2345
Nikhil VJ
  • 141
  • 1
  • 6
2

I gave up on pgrep long ago. Always seemed like it should be more useful that what it ends up being.

This is what I use. A bit long winded, and always thought there must be a better way, so maybe someone can enlighten us both.

egrep allows for filtering with extended regular expressions which becomes important when you only want "one answer". grep -E is supposed to do the same thing but I got in the habit of using egrep because I wanted a solution which worked across multiple flavors of *nix.

The -e flag tells ps to show all processes (runs with permission of executing user. ie: not all procs show unless you are root). The-f flag tells ps to issue a "full-format" listing which you might need to | less when viewing it since longer CLI args might scroll off the right of the terminal and might not wrap. Hope it helps.

Find processes matching agetty:

# ps -ef | egrep agetty
root      1386     1  0 Sep19 ttyS0    00:00:00 /sbin/agetty --keep-baud 115200 38400 9600 ttyS0 vt220
root      1388     1  0 Sep19 tty1     00:00:00 /sbin/agetty --noclear tty1 linux
root     28332  4228  0 11:23 pts/2    00:00:00 grep -E agetty

Find process matching agetty that also have baud in them:

# ps -ef | egrep 'agetty.*baud'
root      1386     1  0 Sep19 ttyS0    00:00:00 /sbin/agetty --keep-baud 115200 38400 9600 ttyS0 vt220
root     28347  4228  0 11:23 pts/2    00:00:00 grep -E agetty.*baud

Don't return my grep process. Not sure how it works but saves you from having to | grep -v 'grep' on the results. Maybe someone could explain this:

# ps -ef | egrep 'agetty.*b[a]ud'
root      1386     1  0 Sep19 ttyS0    00:00:00 /sbin/agetty --keep-baud 115200 38400 9600 ttyS0 vt220

egrep with the OR condition

# ps -ef | egrep '(ag[e]tty|ac[p]id)'
root       944     1  0 Sep19 ?        00:00:00 /usr/sbin/acpid
root      1386     1  0 Sep19 ttyS0    00:00:00 /sbin/agetty --keep-baud 115200 38400 9600 ttyS0 vt220
root      1388     1  0 Sep19 tty1     00:00:00 /sbin/agetty --noclear tty1 linux
root     28395  4228  0 11:28 pts/2    00:00:00 grep -E (agetty|acpid)
Server Fault
  • 3,454
  • 7
  • 48
  • 88