How to use sleep function in Python to wait for 10 minutes and check again and run the script?

We have scheduled a cron job to start 5 Elastic Clusters at 7:30 AM, and we have to another cron job scheduled to start a script in spark cluster at 8:00 AM.

In order to run the script in spark cluster, The 5 elastic clusters status should be green. Currently, I am monitoring the ES clusters, if all 5 clusters are not green, i am disabling the cronjob to run the script in spark cluster.

I have defined the below methods to find ES clusters status and to find out the count of how many ES clusters are green.

I am using the below logic to check if all(5) ES clusters are green trigger the function if not exit.

def get_es_clusters_status(elastic_load_balancer)
def count_green_es_clusters(es_clusters_status)
es_clusters_status = get_es_clusters_status(lb_instances)
    if green_es_clusters_count == 5:
        print("YES There are 5 green clusters")
                 print(" Not all 5 clusters are green exiting....")

But I would like to use sleep functionality from time module and wait for 10 minutes and check the Green ES clusters count again if they are 5 trigger the function otherwise wait for 10 more minutes. Unless there are 5 ES clusters i should not trigger the script in spark cluster.


  • The answer to your problem is fairly simple. You can use an if else statement to check if all Clusters are green, and in the else section do the sleep. Here is what the if else statement would look like:

    while clusters!=green:
        time.sleep(600) #This sleeps for 600 seconds, which is 10 minutes. 
        if clusters==green:
            break  #The last 2 lines are not needed but are a foolproof way to make sure your code breaks. 

    Make sure you import time at the top of your code.