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

  1. Save the script to a file, for example with nano restartscript.sh, and paste the code in.
  2. Make it executable:
    chmod 755 restartscript.sh
    Do not use chmod 777 here: 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.
  3. 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/file with the directory where you saved the script.
  4. 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).

Related services

KVM VPS hosting

NVMe SSD and full root access, with Free Basic Managed Support.

Unmanaged dedicated servers

full root access and free IPMI KVM, with Free Basic Managed Support.

Managed dedicated servers

our engineers look after the server for you, from $135 a month.

Was this answer helpful?

Still stuck? Open a ticket