Home > Back-end >  List all matching processes using regex in Linux
List all matching processes using regex in Linux

Time:12-22

I am looking for a regular expression in Linux to filter processes. My requirement is to build a single regex to filter processes with the below 3 conditions:

  • Find processes with the given username
  • Find processes where a particular word matches
  • Ignore processes where a particular word matches

Let's say I have the following 4 processes running on my server:

user1  2683  1  0 Dec03 ?        00:06:28 java -Xms2g -Xmx48g -DlogDir=. -DuploadDir=. -jar webapp-runner-8.0.33.4.jar -AconnectionTimeout=3600000 --port 8080 app1.war
user2 26568  1  1 06:32 pts/3    00:00:32 java -Xms2g -Xmx12g -DlogDir=. -DuploadDir=. -jar webapp-runner-9.0.31.0.jar -AconnectionTimeout=3600000 --port 9010 app2.war
user3 89568  1  1 06:32 pts/3    00:00:28 java -Xms2g -Xmx12g -DlogDir=. -DuploadDir=. -jar webapp-runner-9.0.31.0.jar -AconnectionTimeout=3600000 --port 9020 app3.war
user2 12657  1 22 Nov21 ?        1-05:51:45 java -Xmx1g -jar entrypoint.jar MS_ENV=dev MS_NAME=dev-cron MS_ID= MS_CLASS=com.test.cron.Cron

Now say from the above processes, I want to filter processes with a username user2 and user3, get processes where the word java matches and ignore processes where word cron matches.

The output of regex should be:

user2 26568  1  1 06:32 pts/3    00:00:32 java -Xms2g -Xmx12g -DlogDir=. -DuploadDir=. -jar webapp-runner-9.0.31.0.jar -AconnectionTimeout=3600000 --port 9010 app2.war
user3 89568  1  1 06:32 pts/3    00:00:28 java -Xms2g -Xmx12g -DlogDir=. -DuploadDir=. -jar webapp-runner-9.0.31.0.jar -AconnectionTimeout=3600000 --port 9020 app3.war

Can someone please help me out with this?

CodePudding user response:

It is probably easier to use just pgrep:

pgrep -u user2,user3 -f java

Pipe it to whatever process list you like to generate:

pgrep -u user2,user3 -f java | xargs ps wu

CodePudding user response:

Try this:

ps -ef | grep -E 'user2|user3' | grep 'java' | grep -v 'cron'

CodePudding user response:

You can do that by matching the regular expression

^(?=.*\b(?:user1|user2)\b)(?=.*\d{2}:\d{2}:\d{2}  java\b)(?!.*\bcron\b).*$

with general, case insensitive and multiline flags set.

Demo

This expression can be broken down as follows (and/or hover the cursor over each element of the expression at the link to obtain an explanation of its function).

^                     # match beginning of a line
(?=                   # begin a positive lookahead
  .*\b                # match >= 0 characters then a word boundary
  (?:user1|user2)     # match 'user1' or 'user2'
  \b                  # match a word boundary
)                     # end positive lookahead
(?=                   # begin a positive lookahead
  .*                  # match >= 0 characters
  \d{2}:\d{2}:\d{2}   # match a time stamp then > 0 spaces
  java\b              # match 'java' followed by a word boundary
)                     # end positive lookahead
(?!                   # begin a negative lookahead
  .*                  # match >= 0 characters
  \bcron\b            # match 'cron' with word boundaries
)                     # end negative lookahead
.*$                   # match a line

Note that the lookarounds do not advance the regex engine's string pointer from the start of the string.

  • Related