4

I have a script (in a .sh file) to run once everytime i logged in. But I have to make sure that mysql service is already running before the main script in my .sh executed.
Here is my draft:

function check_mysql(){
    // script for checking mysql
    return status
}
mysqlstatus = check_mysql()
while mysqlstatus == false {
    mysqlstatus = check_mysql()
}
// once mysqlstatus == true
// my main script
// my main script
// my main script
// my main script

How to write the above draft in shell script?

4

Finally I found the answer! :D

while !(mysqladmin ping)
do
   sleep 3
   echo "waiting for mysql ..."
done
echo "starting the main script"
#main script
#main script
#main script
  • 1
    Be careful with many iterations on shell loop. Se this issue. – gwarah Jul 12 '16 at 15:21
3

THANK YOU for bringing that mysqladmin ping command to my attention!

It's going to be a big help when dealing with Docker.

Here's what I ended up doing:

    # Returns true once mysql can connect.
    mysql_ready() {
        mysqladmin ping --host=database --user=root --password=MYSQL_ROOT_PASSWORD > /dev/null 2>&1
    }

    while !(mysql_ready)
    do
       sleep 3
       echo "waiting for mysql ..."
    done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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