Shell Script - How to run script after mysql is ready?

291

Solution 1

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

Solution 2

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
Share:
291

Related videos on Youtube

KiddoDeveloper
Author by

KiddoDeveloper

Updated on September 18, 2022

Comments

  • KiddoDeveloper
    KiddoDeveloper over 1 year

    I am learning Kubernetes. I have a question related to the K8s run command.

    Command: kubectl run testpod --image=nginx:alpine

    As per K8s documentation., run command creates a deployment or job to manage the containers.

    Question: When I run the above commands, I can not see any new deployment. Why? Is it creating only pod?

    Question: I am using Docker desktop. When I run above command, it is showing two different instances in docker desktop. Why two different instances?

    enter image description here

    Question: If I delete any instance from Docker desktop, a new instance is automatically generated. Why? I knew about deployment like if deployment behind of pod and that pod is down or deleted, deployment will automatically create a new pod. But, I cannot any deployment there.

    • Gayan Weerakutti
      Gayan Weerakutti over 8 years
    • Oki Erie Rinaldi
      Oki Erie Rinaldi over 8 years
      I need the script to run at the startup. So it must wait for mysql service. Good reference anyway ..
    • ItayB
      ItayB over 2 years
      did you try kubectl get deployment --all-namespaces ?
    • KiddoDeveloper
      KiddoDeveloper over 2 years
      Yes, I knew this command and already cross-checked it. This command is showing only 1 deployment i.e.'kube-system'
    • ItayB
      ItayB over 2 years
      what about replicaset?
    • Arutsudar Arut
      Arutsudar Arut over 2 years
      Can you check all the resources using the command kubectl get po,rs,rc,deploy. If possible attach the output of this. (May be at least the ones which have the name testpod.) We can see if any ReplicationSet or ReplicationController or Deployment is available in your cluster.
    • Arutsudar Arut
      Arutsudar Arut over 2 years
      Can you check what is the kind displayed in the yaml, when you run the command kubectl run testpod --image=nginx:alpine --dry-run -o yaml. If possible attach this output as well.
  • gwarah
    gwarah almost 8 years
    Be careful with many iterations on shell loop. Se this issue.