Sometimes it is needed to understand the average amount of memory consumed by a process. You can create a shell script that’ll do the job for you. I’ve tested it on Ubuntu and it works fine (not sure about other OS). So, in the command line type:
nano avmem.sh
In the window opened, paste the following text:
echo “Enter the process name (i.e. apache2):”
read process
ps -ylC $process | awk ‘{x += $8;y += 1} END {print “Process Memory Usage (MB): “x/1024; print “Average Proccess Size (MB): “x/((y-1)*1024); print “Total Number of Processes: “(y-1)}’
Then press Ctrl+x and answer yes when prompted.
Your script is ready! You can now run it by executing the following in the command line:
./avmem.sh
It’ll ask you to enter the name of a process (i.e. apache2 for Apache, mysqld for MySQL, nginx for nginx, you name it) and give you information about number of processes running, total amount of memory consumed by these processes and average amount of memory consumed by one process instance.
Hope it helps!
Don’t forget to give execution rights to your script by this command:
chmod +x avmem.sh.
And it’s desirable in shell scripts to use first line as path to your interpretator:
#!/bin/sh