Shell Script to Calculate Memory Usage by A Process

The following script will calculate the Total Memory Usage by A process in Megabytes and the number of threads.

You have to supply the string that will identify the process in ps aux output.

sample usage  calc_memory.sh  apache

!/bin/bash STRING=$1 if [ -z "$STRING" ]; then echo "Usage:calc_memory " exit 0 else USAGE_PRCNT=ps aux | grep ${STRING} | grep -v grep | awk '{print $4}' | awk '{total = total +$1}END{print total}' NO_THRDS=ps aux | grep ${STRING} | grep -v grep | wc -l SYS_MEM=cat /proc/meminfo | grep MemTotal | awk '{print $2}' USG=echo "$SYS_MEM*$USAGE_PRCNT/102400" |bc; echo Total Usage By ${STRING}:${USG}MB echo Total Number of Threads:${NO_THRDS} fi