Linux memory script

Below is a simple script to check the total memory and cpu used by process

MY_USER=`id -u -n`
> /tmp/my_mem_$MY_HOST.txt
echo `date` >> /tmp/my_mem_$MY_HOST.txt
ps -e -o rss,vsz,pcpu,user,args | grep $MY_USER > /tmp/$MY_HOST_prcs.txt
cat /tmp/$MY_HOST_prcs.txt | awk ‘{a=$1;b=$2;c=$3;x=x+a;y=y+b;z=z+c;ma=x/1024;mb=y/1024;{printf(”   Resident memory size is:   %s MB\n   CPU for Processes:        %s %%\n   Virtual memory size is:    %s MB\n”,ma,z,mb)}}’  | tail -3 >> /tmp/my_mem_$MY_HOST.txt
cat /tmp/my_mem_$MY_HOST.txt

which will report the total memory and virtual memory used together with CPU % by the particular user.

Mon Jul 25 01:45:40 EDT 2016
Resident memory size is:   12664.8 MB
CPU for Processes:        19.4 %
Virtual memory size is:    49755.9 MB

Can be modified and place inside a loop for monitoring.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s