Here is the script to restart a server automatically if it’s load increases above a limit :
#!/usr/bin/ bash
LOAD=`uptime |awk '{print $NF}'`
LOADCOMP=`echo $LOAD \> 10 |bc -l`
if [ $LOADCOMP -eq 1 ]
then /sbin/shutdown -r 0
fi
Use sudo if you have not permission
#!/usr/bin/ bash
LOAD=`uptime |awk '{print $NF}'`
LOADCOMP=`echo $LOAD \> 10 |bc -l`
if [ $LOADCOMP -eq 1 ]
then sudo /sbin/shutdown -r 0
fi
Change the permisssion of file
# chmod 777 restartscript.sh
Or
$ sudo chmod 777 restartscript.sh
- Now we need to add this script to cron which will run every 2 minutes
- Type crontab -e to open the cron input file.
- Enter the following code to run the script we just created
*/2 * * * * cd /home/ubuntu/public_html/scripts && sh ./restartscript.sh > /home/ubuntu/public_html/cronlogfile.log 2>&1
To check if cron is added succesfully run crontab -l
to list all the crons