Alan Alan - 1 year ago 102
Python Question

Bash/Python processes matching

I can use

pgrep -f 'keyword1 | keyword2'

to run a pgrep and return all processes that match either keyword.

How can I use & to do this instead? I just want processes that contain both keywords

The following patterns failed:

pgrep -f 'keyword1 & keyword2'
pgrep -f 'keyword2 && keyword2'

MAN pgrep(1)


-f The pattern is normally only matched against the process
name. When -f is set, the full command line is used.


Side question:
Is there a built in Python library for running these commands? I couldnt seem to find one and everyone suggested using subprocess.Popen(), which is how I'm running the 'pgrep' command, however I'd prefer a pure Python solution if it's available

Answer Source

You can use or with a wildcard reversing the pattern to get either keyword in any order.

pgrep 'keyword1.*keyword2|keyword2.*keyword1'