Diego Di Tommaso
Diego Di Tommaso

Reputation: 155

Python Loop at scheduled hours

I need to iterate some functions in Python every 5th minute of the day (09:00:00; 09:05:00; 09:10:00; ...). I guess that if I use the sleep function I would accumulate all the calculations time, slowly moving away from the 5th minute in execution. Is there a way to overcome this problem?

Thanks!!

Upvotes: 1

Views: 111

Answers (2)

martin
martin

Reputation: 96979

You could always check current time before scheduling another call and set the delay:

delay = (5 * 60 * 1000) - time_since_last_function_call

which will always result into something under 5 minutes. In python there's also sched module with similar functionality.

Alternative and probably better solution would be to use crontab:

*/5 9 * * * /bin/python /your/script/path.py

Upvotes: 1

Eric
Eric

Reputation: 2739

Actually you can use crontab in Linux to run a python script printing the time stamp every 5th minute. The following code is an example of using crontab

*/5 * * * * /usr/bin/python your_python_script.py

Hope it helps.

Upvotes: 1

Related Questions