Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndreiTelteu/5bcc9bcc6e4fcf3e3e8dd02c2e18fd43 to your computer and use it in GitHub Desktop.
Save AndreiTelteu/5bcc9bcc6e4fcf3e3e8dd02c2e18fd43 to your computer and use it in GitHub Desktop.
Bash script to restart the server if the load is greater than x

Put the following script in a .sh file, for example in /root/reboot-load.sh.

#!/usr/bin/bash
LOAD=`uptime |awk '{print $NF}'`
LOADCOMP=`echo "($LOAD * 100) / 1" | bc`
echo "Load is: $LOAD"
if [[ $LOADCOMP -ge 2000 ]]
then echo "Greater than 20 ! Reboot !"
fi
if [[ $LOADCOMP -ge 2000 ]]
then reboot
fi

As the user root, run command crontab -e and add a cron job for this:

*/5 * * * * bash /root/reboot-load.sh

This will check every 5 minutes if the average load for 15 min is greater than 20.00


If you want to change the limit, for example to 1.52 load, multiply by 100 (result will be 152) and change it in the lines with if:

if [[ $LOADCOMP -ge 152 ]]

If you want to use the load average for 5 min replace the second line with this:

LOAD=`uptime |awk '{print $(NF-1)}' FS=,`

If you want to use the load average for 1 min replace the second line with this:

LOAD=`uptime |awk '{print $(NF-2)}' |awk '{print $1}' FS=,`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment