Wednesday, March 20, 2013

Restart any linux service at set load

Create a SH script in a desired folder.

vi /root/scripts/restart_service_high_load.sh

Paste

#!/bin/sh
# restart service at high load
 

load_check=`cat /proc/loadavg | sed 's/\./ /' | awk '{print $1}'`

#Max load for restart
load='Set max load you want'
 

# log the restarts
restart_log='PATH TO LOG FILE e.g /root/restart_log';


# location to
service_script='PATH TO SERVICE e.g /etc/init.d/httpd';



if
        [ $load_check -gt "$load" ]; then
        $service_script stop
        sleep 1;
        $service_script restart
        echo "$(date) : $service_script restarted  | load was $check |" >> $restart_log;


fi


Create a cron to run at when you want it to
For example (every 5 mins) */5 * * * * /root/scripts/restart_service_high_load.sh >/dev/null 2>&1

No comments:

Post a Comment