Updated: 22 October 2026 · Applies to: any Linux VPS or dedicated server with bash and bc installed
This script reboots the server automatically if its load average goes above a limit you set. Treat it as a stopgap for sudden load spikes, not a fix: if the server keeps hitting the limit, you still need to find and resolve whatever is actually driving the load up.
#!/usr/bin/bash
LOAD=`uptime | awk '{print $NF}'`
LOADCOMP=`echo $LOAD \> 15 | bc -l`
if [ $LOADCOMP -eq 1 ]
then
/sbin/shutdown -r 0
fi
The 15 here is the load threshold; adjust it to whatever makes sense for your server's CPU count and normal load.
Set it up
- Save the script to a file, for example with
nano restartscript.sh, and paste the code in. - Make it executable:
chmod 755 restartscript.sh
Do not usechmod 777here: that also makes the script writable by every user on the system, so anyone with a shell account could edit it to run arbitrary commands as root the next time cron fires it. 755 is enough to run it. - Add it to cron to run every 2 minutes:
crontab -e
*/2 * * * * cd /path/to/file && sh ./restartscript.sh > /var/log/cronlogfile.log 2>&1
Replace/path/to/filewith the directory where you saved the script. - Confirm the cron job is there:
crontab -l
With this in place, the server reboots whenever its load average passes 15 (or whatever limit you set).