regexlinuxshellgrepprocps

pgrep multiple process names


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?


Solution

  • 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