Home > Linux Command Line, Scripts > Linux bash Script to record all process during high load

Linux bash Script to record all process during high load

 

 

You can use the following script to record all running processes in a system during high load. The script saves all processes information to the /tmp/pinfo directory and sends an email alert to “alertadmins@gmail.com”. The report can be reviewed later to find out the processes running during that time.

 

#!/bin/bash 
EMAIL="alertadmins@gmail.com"

if [ ! -d /tmp/pinfo ];then 
   mkdir /tmp/pinfo 
fi 

if [[ `awk '{print int($3)}' /proc/loadavg` -gt `grep -c processor /proc/cpuinfo` && ! -f /tmp/pinfo/.lock ]];then
   touch /tmp/pinfo/.lock 
   #Record all process 
   filename = "/tmp/pinfo/`date +%F%s`"
   top -b -n 1 > ${filename}
   pstree -plan >> ${filename}
   vmstat 1 7 >> ${filename}
   rm -f /tmp/pinfo/.lock 
   #Send alert email
   msg = "High Load on `hostname`\nLoad : `cat /proc/loadavg`\nCheck ${filename} for more details"
   echo -e ${msg}|mail -s "Load alert from `hostname`" ${EMAIL}
fi

 

 

If you want the cron to run every minute starting at 11AM server time and ending at 3PM , then set the cron as follows:

 

* 11-15 * * * /root/loadmon.sh
Advertisements
  1. No comments yet.
  1. No trackbacks yet.

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: