Gerard Seibert wrote:
I want to run a script from CRON that will check to see if MySQL is running, and if not, restart it. I have had a problem with MySQL shutting down unexpectedly.
This is my first attempt at writing the script. #!/bin/sh if (`ps -wxuU mysql | grep -o mysqld_safe`) then echo "MySQL is Running" else /usr/local/etc/rc.d/mysql-server.sh restart echo "MySql Server is Restarted" fi
if tests the exit status of the commands it runs, so this would be a better way to code that: if ps -wxuU mysql | grep -o mysqld_safe >/dev/null 2>&1 ; then echo "MySQL is running" else /usr/local/etc/rc.d/mysql-server.sh restart && \ echo "MySQL restarted" fi However, grepping the process list is not the best way to findif a process is still running. Daemon processes generally have a pid file, and you can test that a process with that PID is running by using kill(1):
if kill -0 $( cat /var/db/mysql/$( hostname ).pid ) ; then ... But then you usually have to worry about coping with the pid file being absent. However, since you're worrying about mysql, one ofthe best tools to find out if mysql is running is mysqladmin(1). Try running:
mysqladmin ping
(possible with a few more arguments to specify exactly how to contact the mysql server and what DB userid to use) as your test that theserver is still alive.
Note that if MySQL is crashing it may well leave database tables in a damaged state: automatically restarting mysql in that case isn'tgoing to me very productive. mysqlcheck(1) is your friend in this situation.
Your best strategy is really to work out why MySQL is crashing and take steps to stop it. It would be unusual for mysql to die without leaving some sort of clue in the error log (by default /var/db/mysql/`hostname`.err) and you can always turn on the query log (or the bin log, which is equivalent, but needs a separate program to display its contents in a readable form) to see exactly what was happening around the time of the crash. One big reason for MySQL to crash is incorrect sizing of various buffers and internal arrays. Another is if the process tries to grow beyond the maximum possible size -- 128MiB by default, but can be tuned by setting kern.maxdsiz in loader.conf or by eg. 'options MAXDSIZ=(1024UL*1024*1024)'in your kernel configuration.
Cheers, Matthew -- Dr Matthew J Seaman MA, D.Phil. 7 Priory Courtyard Flat 3 PGP: http://www.infracaninophile.co.uk/pgpkey Ramsgate Kent, CT11 9PW
signature.asc
Description: OpenPGP digital signature