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
#!/bin/bash
#
/usr/lib/nagios/plugins/check_mem.pl -u -C -w 80 -c 90 > /dev/null 2>&1
if [ $? -gt 0 ]; then
  NOW=$(date +"%Y-%m-%d-%H-%M-%S")
  LFILE=/tmp/mem_${NOW}.txt
  #echo "Restaring Apache on `hostname`"
  LMSG="Restaring Apache on `hostname`"
  LSUB="High Memory on `hostname`"
  echo "$LMSG" | mailx -s "$LSUB" system@example.org
  ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem > $LFILE
  sudo /bin/systemctl stop apache2.service
  sleep 60
  sudo /bin/systemctl start apache2.service
fi

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