From owner-freebsd-questions@FreeBSD.ORG Sat Dec 17 20:51:13 2005 Return-Path: X-Original-To: freebsd-questions@freebsd.org Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 3BC5616A41F for ; Sat, 17 Dec 2005 20:51:13 +0000 (GMT) (envelope-from m.seaman@infracaninophile.co.uk) Received: from smtp.infracaninophile.co.uk (imap.infracaninophile.co.uk [81.187.76.162]) by mx1.FreeBSD.org (Postfix) with ESMTP id A6BA243D86 for ; Sat, 17 Dec 2005 20:50:55 +0000 (GMT) (envelope-from m.seaman@infracaninophile.co.uk) Received: from [IPv6:::1] (localhost [IPv6:::1]) by smtp.infracaninophile.co.uk (8.13.4/8.13.4) with ESMTP id jBHKofcL070128 for ; Sat, 17 Dec 2005 20:50:41 GMT (envelope-from m.seaman@infracaninophile.co.uk) Message-ID: <43A47A1B.6070203@infracaninophile.co.uk> Date: Sat, 17 Dec 2005 20:50:35 +0000 From: Matthew Seaman Organization: Infracaninophile User-Agent: Mozilla Thunderbird 1.0.7 (X11/20051204) X-Accept-Language: en-us, en MIME-Version: 1.0 To: freebsd-questions@freebsd.org References: <20051217141021.1B66.GERARD@seibercom.net> In-Reply-To: <20051217141021.1B66.GERARD@seibercom.net> X-Enigmail-Version: 0.93.0.0 Content-Type: multipart/signed; micalg=pgp-ripemd160; protocol="application/pgp-signature"; boundary="------------enig85DA0BE801A6E7943D328300" X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-2.0.2 (smtp.infracaninophile.co.uk [IPv6:::1]); Sat, 17 Dec 2005 20:50:41 +0000 (GMT) X-Virus-Scanned: ClamAV 0.87.1/1211/Fri Dec 16 22:51:35 2005 on happy-idiot-talk.infracaninophile.co.uk X-Virus-Status: Clean X-Spam-Status: No, score=-2.5 required=5.0 tests=AWL,BAYES_00,NO_RELAYS autolearn=ham version=3.1.0 X-Spam-Checker-Version: SpamAssassin 3.1.0 (2005-09-13) on happy-idiot-talk.infracaninophile.co.uk Subject: Re: Script Problem X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 17 Dec 2005 20:51:13 -0000 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--