Search code examples
mysqllinuxapacheshellcentos

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 3 AM.

I'm doing this:

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

Is this the correct way to do it? I'm stopping and starting mysql and httpd because use of memory.

Should I do some check for memory before stopping them, like "if memory is less than X stop them", or can I do it without problems?


Solution

  • 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.