My server always shut down when it reach a high load average. I have optimized my Apache, MySQL. But I cannot prevent sometime in a heavy traffic. So I try to write some shell to control load average.
#!/bin/bash
check=`uptime | sed 's/\./ /' | awk '{print $10}'`
if[$check -gt 5]
then
/usr/bin/systemctl restart httpd.service
fi
but it report error when I excuse the script
/var/www/html/load_average.sh: line 3: if[0.98, -gt 5]: command not found
/var/www/html/load_average.sh: line 4: syntax error near unexpected token `then'
/var/www/html/load_average.sh: line 4: `then'
Another question, how to run the script in every 10 seconds with cronjob?
another working code, share to everyone:
#!/bin/bash
check=$(uptime | tr -d ',' | awk '{print $10}')
if [[ $check > 5 ]]; then
/usr/bin/systemctl restart httpd.service
fi
in cronjob part
* * * * * /var/www/html/load_average.sh >/dev/null 2>&1
* * * * * sleep 10; /var/www/html/load_average.sh >/dev/null 2>&1
* * * * * sleep 20; /var/www/html/load_average.sh >/dev/null 2>&1
* * * * * sleep 30; /var/www/html/load_average.sh >/dev/null 2>&1
* * * * * sleep 40; /var/www/html/load_average.sh >/dev/null 2>&1
* * * * * sleep 50; /var/www/html/load_average.sh >/dev/null 2>&1