Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 17 Dec 2005 17:43:25 -0600
From:      Paul Schmehl <pauls@utdallas.edu>
To:        freebsd-questions@freebsd.org
Subject:   Re: Script Problem
Message-ID:  <AD8571922C872ADE11DFBC7A@Paul-Schmehls-Computer.local>
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
--On December 17, 2005 2:13:04 PM -0500 Gerard Seibert 
<gerard@seibercom.net> 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/



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