Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 17 Dec 2005 20:50:35 +0000
From:      Matthew Seaman <m.seaman@infracaninophile.co.uk>
To:        freebsd-questions@freebsd.org
Subject:   Re: Script Problem
Message-ID:  <43A47A1B.6070203@infracaninophile.co.uk>
In-Reply-To: <20051217141021.1B66.GERARD@seibercom.net>
References:  <20051217141021.1B66.GERARD@seibercom.net>

next in thread | previous in thread | raw e-mail | index | archive | help
This is an OpenPGP/MIME signed message (RFC 2440 and 3156)
--------------enig85DA0BE801A6E7943D328300
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

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

--------------enig85DA0BE801A6E7943D328300
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (FreeBSD)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFDpHog8Mjk52CukIwRA3/PAJwKQ5Tbs1QPbhB+8HrXZDN9UG4NBACeJ4Mj
oX7ouNYGVc3jsSl7cl9Pbg0=
=X26f
-----END PGP SIGNATURE-----

--------------enig85DA0BE801A6E7943D328300--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?43A47A1B.6070203>