From owner-freebsd-questions@FreeBSD.ORG Sat Dec 17 23:44:14 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 31E2E16A41F for ; Sat, 17 Dec 2005 23:44:14 +0000 (GMT) (envelope-from pauls@utdallas.edu) Received: from mail.stovebolt.com (mail.stovebolt.com [66.221.101.248]) by mx1.FreeBSD.org (Postfix) with ESMTP id 9006843D45 for ; Sat, 17 Dec 2005 23:44:13 +0000 (GMT) (envelope-from pauls@utdallas.edu) Received: from [192.168.2.100] (adsl-66-137-148-194.dsl.rcsntx.swbell.net [66.137.148.194]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.stovebolt.com (Postfix) with ESMTP id C1730114321 for ; Sat, 17 Dec 2005 17:43:23 -0600 (CST) Date: Sat, 17 Dec 2005 17:43:25 -0600 From: Paul Schmehl To: freebsd-questions@freebsd.org Message-ID: In-Reply-To: <20051217141021.1B66.GERARD@seibercom.net> References: <20051217141021.1B66.GERARD@seibercom.net> X-Mailer: Mulberry/4.0.0 (Mac OS X) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline Subject: Re: Script Problem X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: Paul Schmehl List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 17 Dec 2005 23:44:14 -0000 --On December 17, 2005 2:13:04 PM -0500 Gerard Seibert 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 (pauls@utdallas.edu) Adjunct Information Security Officer University of Texas at Dallas AVIEN Founding Member http://www.utdallas.edu/