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?

askedAug 11, 2017 at 3:18
r_dub_'s user avatar
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 ascron, to run your task at a set period, rather than having it run constantly and sleep?CommentedAug 11, 2017 at 3:23

1 Answer1

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 datetimeimport timedelta_hour = 0while:    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
answeredAug 11, 2017 at 3:25
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.