0

I have lots of java processes running on a server and since this is a dev server I have more than one env running.

If I want to list the processes for a particular environment I do this to give a short readable list as the java command is very long with lots of parameters:

ps -ef|grep ENV-NAME|grep -v grep|awk '{print $2 " " $32}'

This prints the process id and the name of the java class that is running.

However, as you can tell, it is dependent on the java class name being in the same column (32) all the time.

If I add an argument to one of the processes it obviously picks up the wrong column and the process list no longer makes sense.

The java class name is always in the form com.company.start.name.

Can I use this info in awk to select the correct column using regex instead of a column number?

2
  • 1
    Worth posting some example input and desired output.
    – steve
    Jan 30, 2020 at 19:37
  • Is what is usually the 32nd field the last field?
    – DopeGhoti
    Jan 30, 2020 at 20:17

1 Answer 1

1

You can't precisely select a column based on the regex, but you could do this:

ps -ef | awk ' /[E]NV-NAME/{ match($0, /[a-z]+\.[a-z]+\.[a-z]+\.[a-z]+/); print( $2 " " substr($0, RSTART, RLENGTH)) }'

I've dropped the greps, made it multiline to be more legible, and you'll probably want to capture all of this in a function.

P.S. I'm also rusty on awk regexes, so you might be able to improve that match statement.

P.P.S. The [E]NV-NAME syntax is a handy shortcut way of keeping a regex from matching itself in ps output. Also useful with grep which, ironically, is not really needed for this particular task.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .