#!/bin/sh
#
# NAME:  MonitorCPU
# 
# USAGE:  Crontab file running every 2 minutes on lcls-prod02
#
#------------------------------------------------------------------
# 
# First kill the firefox or acroread processes that
# are consuming more than 90% CPU 
#
TMPFile=/tmp/MonitorCPU.dat

/bin/rm -f $TMPFile
top -bn 1 | grep -e acroread -e firefox-bin > $TMPFile

while read x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12
do
    if [ "$x5" -ge 90 ]
      then
         kill -9 $x1 
    fi

done < $TMPFile


/bin/rm -f $TMPFile

#
# Kill the firefox and acroread processes that are over
# 10 days old.
#

TenDaysAgo=`date --date="10 days ago" +%b%d`

PIDS="`ps eaxo pid,comm,start_time | egrep "firefox-bin|acroread" | grep -v defunc | grep $TenDaysAgo | awk '{print $1}'`"

#Kill the processes
if [ ! "$PIDS" = "" ]
   then
    for i in $PIDS; do kill -9 $i ; done
fi
