Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a shell scipt and at some point I want to check if mysql is running and start it if it's not. I'm trying the following, but not having any luck:

set mysqlstatus = `sudo /opt/local/bin/mysqladmin5 ping`
if ["$mysqlstatus" != 'mysqld is alive']
then
sudo /opt/local/share/mysql5/mysql/mysql.server start
fi
share|improve this question
    
In shell script, the assignment operator is an = without any spaces. You could probably make this easier by using grep. Also, you don't have a shebang on your script, it could be useful to know what shell you mean to invoke. –  ℝaphink Jan 5 '12 at 15:39
add comment

2 Answers

The script run-one is your friend:

sudo run-one /opt/local/share/mysql5/mysql/mysql.server start

More about run-one script:
http://blog.dustinkirkland.com/2011/02/introducing-run-one-and-run-this-one.html

If you cannot install run-one for whatever reason,
then copy the following code into a new file called run-one:

#!/bin/sh -e
#
#    run-one - run just one instance at a time of some command and
#              unique set of arguments (useful for cronjobs, eg)
#
#    run-this-one - kill any identical command/args processes
#                   before running this one
#
#    Copyright (C) 2010 Dustin Kirkland <kirkland@ubuntu.com>
#
#    Authors:
#        Dustin Kirkland <kirkland@ubuntu.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

PROG="run-one"

# Cache hashes here, to keep one user from DoS'ing another
DIR="$HOME/.cache/$PROG"
mkdir -p "$DIR"

# Calculate the hash of the command and arguments
CMDHASH=$(echo "$@" | md5sum | awk '{print $1}')
FLAG="$DIR/$CMDHASH"

# Handle run-this-one invocation, by killing matching process first
case "$(basename $0)" in
        run-this-one)
                ps="$@"
                # Loop through matching pids
                for p in $(pgrep -u "$USER" -f "^$ps$" || true); do
                        # Try to kill pid
                        kill $p
                        # And then block until killed
                        while ps $p >/dev/null 2>&1; do
                                kill $p
                                sleep 1
                        done
                done
                # NOTE: Would love to use lsof, but it seems that flock()'s
                # are not persistent enough, sometimes; use pgrep/pkill now.
                # pid=$(lsof "$FLAG" | grep "^flock" | awk '{print $2}') || true
                # [ -z "$pid" ] || kill $pid

        ;;
esac

# Run the specified commands, assuming we can flock this command string's hash
flock -xn "$FLAG" "$@"

To give execution permission:

chmod +x run-one

Finally, use ./run-one or set correctly your PATH environment variable to use it without giving the directory.

share|improve this answer
    
Pretty nice answer first timer ;) –  Tim Jan 5 '12 at 16:57
1  
Note that also part of run-one is the keep-one-running tool, which will restart your script if it fails/exits/closes/completes for any reason. –  Dustin Kirkland Jan 14 '12 at 21:18
add comment

You could also look into the software called 'monit'. Syntax & usage is pretty common and simple.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.