Skip to content
Advertisement

Correct way to set a crontab to stop and start mysql and httpd

i’m trying to create a shell script to stop and start mysql and httpd every saturday on 3am, i’m doing it:

myscript.sh:

#!/bin/sh

echo "Stopping MySQL"
service mysqld stop
sleep 1s
echo "Stopping HTTPD"
service httpd stop
sleep 5s
echo "Starting MySQL"
service mysqld start
sleep 2s
echo "Starting HTTPD"
service httpd start

and setting the crontab to:

0 3 * * 6 ~/myscript.sh

It’s correct way to do it? i’m stopping and starting mysql and httpd cuz use of memory, should i do some check before stop them? or i can do it without problems?

another question: how to check memory ram before stop them? like a ‘if’ memory is less than X stop them, something like it?

Thanks in advanced.

Advertisement

Answer

Presumably your MySQL workload comes from your httpd web server.

So, do this, to stop httpd first, then bounce mysqld, then restart httpd.

service httpd stop
sleep 10s
service mysqld restart
service httpd start

But, you should investigate carefully whether this is truly necessary. Lots of production systems don’t need it. Modern Apache servers limit the lifetime of their worker processes automatically, to handle the memory leak situation you are mentioning.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement