Re: Script Problem

2005-12-17 Thread Matthew Seaman

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 21 ;
 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


signature.asc
Description: OpenPGP digital signature


Re: Script Problem

2005-12-17 Thread Paul Schmehl
--On December 17, 2005 2:13:04 PM -0500 Gerard Seibert 
[EMAIL PROTECTED] wrote:



Using FreeBSD 5.4

Please do not laugh. I absolutely suck at writing scripts. Someday I
might learn, but in the mean time, I need some assistance.

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

The reason your script isn't doing what you want it to do is because you're 
using backticks in your if statement.  Essentially what your script says is:


if (please execute this command)
then
do this
else
do this
fi

Which is the same as saying:
if ()
then
do this
else
do this
fi

The script should be saying:

if (this command is successful)
then
do this
else
do this
fi

Remove the backticks and it will work as expected.

You use backticks when you want the results of a command to use somewhere 
else.  For example:

TESTING=`ps -wxuU mysql | grep -o mysqld_safe`
if ( $TESTING )

When you want to test the logic of a script, use echo statements.  That 
will tell you every loop and conditional statement's results without 
actually running anything that might cause problems for you.  Once you're 
sure the conditional or loop is working as you expect, then you can add the 
actual commands.


Paul Schmehl ([EMAIL PROTECTED])
Adjunct Information Security Officer
University of Texas at Dallas
AVIEN Founding Member
http://www.utdallas.edu/
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]