1

I want to pause my code every hour mark, i.e 11:00pm, 12:00am, 1:00am, etc.

How do I go about doing this?

1
  • You need to explain more about what you are doing. How long will it pause? How long will it run? Might it be a better idea to use a scheduler such as cron, to run your task at a set period, rather than having it run constantly and sleep? Commented Aug 11, 2017 at 3:23

1 Answer 1

6

You have two ways to do this:

cron method

Add a cron job for your script that gets executed every hour:

@hourly python /path/to/script

code method

Create an infinite loop within your code that runs every hour (have it sleep for 60 minutes). Or, create an infinite loop within your code that sleeps every minute, have it periodically check what time it is, and if it's on the hour with different delta hour than the current hour, have it run the job.

import datetime
import time

delta_hour = 0

while:
    now_hour = datetime.datetime.now().hour

    if delta_hour != now_hour:
        # run your code

    delta_hour = now_hour

    time.sleep(60) # 60 seconds

    # add some way to exit the infinite loop

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.