Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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']
sudo /opt/local/share/mysql5/mysql/mysql.server start
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

The script run-one is your friend:

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

More about run-one script:

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 <>
#    Authors:
#        Dustin Kirkland <>
#    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
#    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 <>.


# Cache hashes here, to keep one user from DoS'ing another
mkdir -p "$DIR"

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

# Handle run-this-one invocation, by killing matching process first
case "$(basename $0)" in
                # 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
                # 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


# 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
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

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

share|improve this answer

Your Answer


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.