Check High Memory usage and restart Apache

Script to check memory usage and take the following actions if memory is high. Uses nagios plugin to get check memory usage.

  • Sends email to Admin
  • Restarts Apache
  • Saves process list in timestamped file in /tmp directory for reference
/usr/lib/nagios/plugins/ -u -C -w 80 -c 90 > /dev/null 2>&1
if [ $? -gt 0 ]; then
  NOW=$(date +"%Y-%m-%d-%H-%M-%S")
  #echo "Restaring Apach on `hostname`"
  LMSG="Restaring Apache on `hostname`"
  LSUB="High Memory on `hostname`"
  ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem > $LFILE
  #echo "$LMSG" | mailx -s "$LSUB"
  cat $LFILE | mailx -s "$LSUB"
  sudo /bin/systemctl stop apache2.service
  sleep 60
  sudo /bin/systemctl start apache2.service

QR Code
QR Code tech:linux:memory_check (generated for current page)