Query on awk

Hello,

I am trying to find the RAM usage for one of the process from a group of servers.

xx-Process name

The command i used by logging to the individual server is as below

ps aux | grep xx | awk '{sum=sum+$6}; END {print sum/1024 " MB"}'

And the result is

0.824219 MB

This seems to be correct as per the ps command on the server.

ps aux | grep xx
16777248 26166  0.0  0.0 103324   844 pts/0    R+   10:20   0:00 grep xx

Now i implemented via shell script and is giving me a different result.

RAMUsage=$(ssh $i "ps aux | grep xx| awk '{sum+=\$6};END {print sum/1024" MB"}'")
echo "the ramusage is " $RAMUsage

the ramusage is 2.1875

Please help me where it went wrong.

RAMUsage=$(ssh $i "ps aux | grep xx| awk '{sum+=\$6};END {print sum/1024" MB"}'")

what's the \ for?
also:

ps aux | grep xx
16777248 26166  0.0  0.0 103324   844 pts/0    R+   10:20   0:00 grep xx

this is 'grep' from the 'ps' itself - not the process 'xx' you're looking for.

1 Like

I put the backslash to escape the dollar sign.

---------- Post updated at 11:57 AM ---------- Previous update was at 09:59 AM ----------

I understood by comparing with ps aux | grep xx is wrong .

Further to this gave ps aux command in the terminal and found the process am looking has occupied around 218 MB.

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root      2282  0.2  1.3 2411400 218888 ?      Sl   Mar14  54:45 /opt/xx/desktop/xx

But the shell script was giving 2.18359. May be the formula used in script is wrong.Please advise.

Instead of:

ps aux | grep xx

try:

ps aux | grep '[x]x'

To ensure the grep process itself isn't one of the processes being matched.

1 Like

This is less trivial than it seems to be. A process can have several types of memory allocated:

1) real memory
This is real RAM the process uses.

2) virtual memory
This is real memory as above plus allocated but swapped out memory. This actually is what the vsz metric in the ps output shows.

3) shared memory segments
Sometimes several processes use some memory segment collectively. I.e. database software Oracle, DB/2) makes extensive use of this feature. Use the ipcs command to find out if the process owns shared memory segments.

I hope this helps.

bakunin

1 Like