Letus
asked on
Cron script to stop Apache when load is high
Hello,
as I'm experiencing some unknown problem, which causes, that from time to time, the server just freezes, I need to fix this :
1) during the day or night, the server is running fine. Then, for some reason the MySQL databaze is unable to handle all the queries (I did not identified yet WHY), and causes high load, memory is consumed and the server starts to swap, killing processes and usually get into some kind of unstable position.
Until I will be able to fix this problem, I'd like to have a cron job, which will every minute check for the average load of the machine, and if the load is higher than let say 5.00, it issues this command : /etc/init.d/apache2 stop, waits for 10 minutes, and makes /etc/init.d/apache2 start
Can anybody help me, how to do this script ?
Thank you !
as I'm experiencing some unknown problem, which causes, that from time to time, the server just freezes, I need to fix this :
1) during the day or night, the server is running fine. Then, for some reason the MySQL databaze is unable to handle all the queries (I did not identified yet WHY), and causes high load, memory is consumed and the server starts to swap, killing processes and usually get into some kind of unstable position.
Until I will be able to fix this problem, I'd like to have a cron job, which will every minute check for the average load of the machine, and if the load is higher than let say 5.00, it issues this command : /etc/init.d/apache2 stop, waits for 10 minutes, and makes /etc/init.d/apache2 start
Can anybody help me, how to do this script ?
Thank you !
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry, but I forgot to mention that the most important part of the script is the function "checkLoadAvg". =0)
ASKER
Great, I will test it during weekend, but do I run this every minute as a cron job, or does that work as daemon ?
Yes... you run it from cron, and it will do the following:
If the load is higher than 5, it will execute the command specified in the variable "CommandToExecute" plus the word "stop", as a test I put in this variable the following command: "echo Test", but you must change it to: "/etc/init.d/apache2".
After stopping apache, it will sleep the time specified in the variable "SleepTime", and the value I put on it(600), means 10 minutes(10 * 60), after sleeping it will start apache again.
If you want it to run like a "daemon", just comment or remove the "break" and right after it, add "sleep 60", and it will sleep 1 minute. Do not forget to run the script, if executing like a "daemon", with nohup.
I hope it helps. =0)
If the load is higher than 5, it will execute the command specified in the variable "CommandToExecute" plus the word "stop", as a test I put in this variable the following command: "echo Test", but you must change it to: "/etc/init.d/apache2".
After stopping apache, it will sleep the time specified in the variable "SleepTime", and the value I put on it(600), means 10 minutes(10 * 60), after sleeping it will start apache again.
If you want it to run like a "daemon", just comment or remove the "break" and right after it, add "sleep 60", and it will sleep 1 minute. Do not forget to run the script, if executing like a "daemon", with nohup.
I hope it helps. =0)
w|awk '(NR==1&&$(NF-2)>5.0){prin t "/etc/init.d/apache2 stop;sleep 600;/etc/init.d/apache2 start"}'|sh
ASKER
thank you, I will test this too ...
is there any chance, that this scripts echo something into the system log ? (var/log/messages) ? so I know that it was doing something, when I'm away from the server ?
is there any chance, that this scripts echo something into the system log ? (var/log/messages) ? so I know that it was doing something, when I'm away from the server ?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER