Hey everyone
can you check this script logic ?
it has to restart webservice if found server load is higher than X,
also i have put it in crontab to run every one minute
#!/bin/bash
loadavg=$(uptime | awk -F "." '{ print $1 }' | awk -F ":" '{ print $5 }')
if [ "$loadavg" -ge "10" ]; then
pkill -9 httpd
service httpd restart
sleep 5
service varnish restart
loadavg=0
fi
Some comment:
I would stay away from that SIGNAL -9, at least I would find out what could be the side-effects of it.
If httpd is not running because you kill it, service httpd start should be the more appropriated command. Speaking of appropriated, service httpd stop would be a better choice than pkill -9 httpd
It is best to use absolute paths in a cronjob. e.g. /sbin/service httpd start
Interesting you are using a variable named loadavg . The Linux kernel keeps a realtime record of that average load in /proc/loadavg . Reading from that would be cleaner that using uptime.
Let's use awk to handle the floating point as well.
awk '{print ($1>10? 1 : 0)}' /proc/loadavg
The return of awk can be use to follow with a flow control branch
the output of awk is always 1
awk '{print ($1>10? 1 : 0)}' /proc/loadavg
but loadavg is useful in this case at it show the right load,
can you check the result of your line? coz i get 1 in all time