3

I want to get PIDs of two or more processes using "pgrep" command in Linux.

As we know pgrep syntax is

pgrep [options] <pattern>

Here is a hypothetical command which should return PIDs of two processes whose names are process1 and process2 respectively.

pgrep process1 OR process2 

What should be the pattern that needs to be used to achieve the above?

Sven
  • 496
  • 4
  • 9
TechEnthusiast
  • 1,795
  • 2
  • 17
  • 32

1 Answers1

5

Try:

pgrep 'process1|process2'

Example:

 -->pgrep 'atd|cron'
1078
1093

 -->ps -eaf |grep -E 'atd|cron'
daemon    1078     1  0 Aug08 ?        00:00:00 /usr/sbin/atd -f
root      1093     1  0 Aug08 ?        00:00:19 /usr/sbin/cron -f
xxxx  14364  9597  0 11:56 pts/2    00:00:00 grep -E atd|cron
P....
  • 17,421
  • 2
  • 32
  • 52
  • Be careful, as this is totally wrong. Pgrep uses regexp matching, so `atd|cron` can match not only atd & cron but for instance rpc.statd or crond. – Scyld de Fraud Nov 27 '21 at 09:28
  • Its not wrong. Idea for this answer is to tell OP how to use Or condition. When pipe is used for OR condition, it was implied that regex is used. Yes, they need to use word boundaries to prevent false positive. – P.... Nov 27 '21 at 17:59
  • You can just use exact match like this `pgrep -x 'atd|cron'`, or `pgrep '^(atd|cron)$'`. – nggit Nov 09 '22 at 02:59