Script Problem

Matthew Seaman m.seaman at infracaninophile.co.uk
Sat Dec 17 12:51:13 PST 2005


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 find
if 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 of
the 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 the
server 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't
going 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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 254 bytes
Desc: OpenPGP digital signature
Url : http://lists.freebsd.org/pipermail/freebsd-questions/attachments/20051217/18dacb5b/signature.bin


More information about the freebsd-questions mailing list