Script Problem

Paul Schmehl pauls at utdallas.edu
Sat Dec 17 15:44:14 PST 2005


--On December 17, 2005 2:13:04 PM -0500 Gerard Seibert 
<gerard at 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 at utdallas.edu)
Adjunct Information Security Officer
University of Texas at Dallas
AVIEN Founding Member
http://www.utdallas.edu/


More information about the freebsd-questions mailing list