Linux scripts and commands to see RAM/memory utilization

Script to run top command after every 5 minutes RAM utilization:

#!/bin/sh
top -b -n 1 -o %MEM  | head -17 >> /var/log/logging.txt
echo “=========” >> /var/log/logging.txt

Change the permissions to execute,
chmod 777 script1

move the script to cronfrequently to run it after every 5 mins,
mv script1 /etc/actions/cronfrequently/

(use cronhourlybyfour directory for 15 mins execution)

To see the RAM usage by processes:

ps -eo size,pid,user,command | awk ‘{ hr=$1/1024 ; printf(“%13.6f Mb “,hr) } { for ( x=4 ; x<=NF ; x++ ) { printf(“%s “,$x) } print “” }’ | sort

top -b -n 1 -o %MEM  | head -17

To see RAM stats:

vmstat

To see free memory and cached space:

free -hm

To see a specific process CPU or RAM utilization using top::

top -p `pgrep PROCESS_NAME | tr “\\n” “,” | sed ‘s/,$//’`
(e.g. datastore.pl)

Script to see what processes are using  max RAM every 5 minutes:

/var/log/9999monitorram

#!/bin/sh

OutDir=/var/log/monitoring

File=RamUsage$(date +%d-%m-%y)

mkdir -p $OutDir

echo “——————————–$(date +%d-%m-%y)—–$(date +%H:%M:%S)—————————————“>> $OutDir/$File

ps -eo size,pid,user,command | awk ‘{ hr=$1/1024 ; printf(“%13.6f Mb “,hr) } { for ( x=4 ; x<=NF ; x++ ) { printf(“%s “,$x) } print “” }’ | sort | tail -n20>> $OutDir/$File

ln -s 9999monitorram /etc/actions/cronfrequently/

Advertisements

Posted on January 6, 2016, in Linux (Ubuntu/CentOS), Uncategorized. Bookmark the permalink. Leave a comment.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

%d bloggers like this: