Find Memory and activity from command output

Hello -
I have a requirement to get the Memory(Xmx) and the activity name using it.

Sample input info :

1502       02:57 /bin/sh /opt/rather/bar/deploy/bar_run.sh
1545       02:57 java -Drather.repository=/opt/rather/bar/deploy/JobSyng_Barol_Count/JobSyng_Barol_Count/../lib -Xms1024M -Xmx2048M -cp .:/opt/rather/bar/deploy/JobSyng_Barol_Count/JobSyng_Barol_Count:/opt/rather/bar/deploy/JobSyng_Barol_Count/JobSyng_Barol_Count/../lib/routines.jar:/opt/rather/bar/deploy/JobSyng_Barol_Count/JobSyng_Barol_Count/../lib/advancedPersistentLookupLib-1.2.jar:/opt/rather/bar/deploy/JobSyng_Barol_Count/JobSyng_Barol_Count/../lib/rather_file_enhanced_20070724.jar:/opt/rather/bar/deploy/JobSyng_Barol_Count/JobSyng_Barol_Count/../lib/rathercsv.jar:/opt/rather/bar/deploy/JobSyng_Barol_Count/JobSyng_Barol_Count/../lib/trove.jar:/opt/rather/bar/deploy/JobSyng_Barol_Count/JobSyng_Barol_Count/JobSyng_Barol_count_0_1.jar: intlbar_tac.JobSyng_Barol_count_0_1.JobSyng_Barol_Count --context=Default
9738    03:05:12 java -Xms3002M -Xmx3002M -cp /opt/rather/raal/raal.jar com.aexp.raal.main.controller.raalMainController

Command running for the o/p:

ps -eo pid,etime,command | grep rather | egrep -sv "$$|bash|tail|cat|sshd|grep|less|more|su"

Result looking from it (or something similar/meaningful):

ActivityName1:  bar
Memory for ActivityName1:2048M
ActivityName2:  raal
Memory for ActivityName2:3002M

Few pointers :
All these are running under process: rather
The memory is looking under value "Xmx"
always the activity name is after "/opt/rather/"

Can someone help me on the code for me?

Thanks in adv.

With grep 's greedy regex matching its not easy to lay hands on the activity if its "name is after "/opt/rather/". There may be better solutions than these ones that rely on the input lines' structure. Try

ps -eo pid,etime,command | grep -o "/opt/rather/[^ /]*\|-Xmx[^ ]*" | uniq | grep -A1 "\-Xmx"
-Xmx2048M
/opt/rather/bar
-Xmx3002M
/opt/rather/raal

or

ps -eo pid,etime,command | grep -o "\-Xmx[0-9M]* *-cp[ .:]*/opt/rather/[^/]*"  | grep -o "\-Xmx[0-9M]*\|[^ /]*$" | paste -s -d"\t\n"
-Xmx2048M    bar
-Xmx3002M    raal

or

ps -eo pid,etime,command | grep -o "\-Xmx[0-9M]*.\{,30\}/opt/rather/[^ /]*" | grep -o "\-Xmx[0-9M]*\|[^ /]*$" | paste -s -d"\t\n"
-Xmx2048M    bar
 -Xmx3002M    raal

A small awk script might be more apt to fulfill your needs to the full extent...

1 Like

@Rudic , Thank u for the details.

For me the last command worked fine. And wondering , can i assign the o/p to two different variables?
that way I can send to an array and list all of them .

Try

ARRAY=($(ps -eo pid,etime,command | grep -o "\-Xmx[0-9M]*.\{,30\}/opt/rather/[^ /]*" | grep -o "\-Xmx[0-9M]*\|[^ /]*$" | tr "\n" " "))
echo ${ARRAY[@]}
-Xmx2048M bar -Xmx3002M raal