Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I need to run a python script at various times depending on the day. The requires are:
On Weekdays:
Start 7 am - End 12 pm => run script every 5 minutes
Start 12:00 pm - End 4 pm => run script every 30 minutes
Start 4 pm - End 9 pm => run script every 5 minutes
Start 9 pm - End 7 am => run script every hour
On Weekends:
Run script every half hour
I've done some simple cron stuff before, but it doesn't seem like cron can handle this kind of granularity.
I've thought about creating a bash script where it would check the time and run the python script if conditions are met, but I run into problems of how to persist time through each run of the bash script.
Just register multiple entries in your crontab:
#
# Weekdays
#
# Start 7 am - End 12 pm => run script every 5 minutes
*/5 7-11 * * mon-fri * yourscript
# Start 12:00 pm - End 4 pm => run script every 30 minutes
0,30 12-15 * * mon-fri * yourscript
# Start 4 pm - End 9 pm => run script every 5 minutes
*/5 16-20 * * mon-fri * yourscript
# Start 9 pm - End 7 am => run script every hour
0 0-6,21-23 * * mon-fri * yourscript
#
# Weekends
#
# Run script every half hour
0,30 * * * sat-sun * yourscript
Cron can handle such granularity just fine.
If you are working in Windows, there is a program called task scheduler that allows you to do exactly this.
If you are working in Linux(which it sounds like you are), I believe crontab will do what you want. Here is a tutorial that I found, I hope that it is helpful to you.
Have a look at apscheduler: http://pythonhosted.org/APScheduler/
and dateutil.rrule: http://labix.org/python-dateutil
With those two you can make a lot of chron like rules from within python, which makes it more easy to transfer between machines and OSes.. :)
Related
This question already has answers here:
How do I get a Cron like scheduler in Python?
(9 answers)
Closed last month.
I need to run a python script at specific time at EVERY minutes. I CANNOT use time.sleep to wait 60 second for it, because my script takes 8-10 seconds to run, (60 + 8 = 68 or 60+10 = 70)
I need to run the script at below specific time through out the day.
2023-01-09 01:01:05
2023-01-09 01:02:05
2023-01-09 01:03:05
2023-01-09 01:04:05
2023-01-09 01:05:05
I do lots of search and can't find a good answer. Worse case should i create a dataframe to tell the script based on the time?
besides, in window Task Scheduler,I don't see there's a option to set 1 Minute recurring.
thanks
You can calculate the time when it should next run and then do:
time.sleep(nextime - datetime.now())
I have a batch script, which I initiate each day at 7am. I want this to run no longer than (approximately) 12 hours. During this 12 hour period, I want this batch script to run a python file a random number of times (between 4 and 8).
So far, I have this batch script to divide the 12 hour period by the random number of times, and run the python file with an equal time delay between each. This is working nicely.
However, I would like to try to take this a step further, whereby the Python file is run with a random time delay (subject to a minimum) between each run, and the total delay during the day equals 12 hours.
I am not sure how to do this, but have an idea to start. I was thinking of having a delay 'pot' of 12hrs * 60mins = 720mins. First calculating a random number, say X, between 4 and 8. Then I need to calculate X number of random time delays which all add up to 720 and each delay to be a minimum of 30.
Any help or pointers would be appreciated.
So far, my batch script looks like this:
REM Generate random integer between 4 and 8
set /a rand_n=%random% %%5 + 3
echo %rand_n%
REM Calculate interval
set /a interval=(12*60*60)/(%rand_n%)
echo %interval%
REM for /l %%x in range(%rand_n%) do (
for /l %%i in (1,1,%rand_n%) do (
REM Delay
timeout %interval%
REM Run Python script
py python_file.py
)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
Have run into more of a maths problem than code. So far have programmed to retrieve the local time, print the local time as "Current Time: " and then programmed the variables to print "Number of minutes since midnight: ".
looks like this
import time
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
print("Current Time: ",current_time)
hour = time.strftime("%H")
minute = time.strftime("%M")
print ("Number of minutes since midnight: ",int(hour)*60+int(minute))
so my output is
Current Time: 22:16:15
Number of minutes since midnight: 1336
Except a quick google search tells me it's closer to 2,777 minutes since midnight.
This is my first program, so if you're wondering why I want to know how many minutes since midnight it is at any given time without JFGI, I just do. It's been a fun problem to solve so far, and I would hate to leave it unfinished because I don't know the maths I need to know yet.
Thanks in advance!
I think your code is actually working as intended!
One quick recommendation for an improvement though:
When you call
t = time.localtime()
You're storing the current time in the variable t, but then when you later call
hour = time.strftime("%H")
minute = time.strftime("%M")
you are actually asking the program to fetch the system clock time again twice, rather than using the already stored time value you have in t.
You could instead do the following and access the hour and time values from the t object directly.
import time
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
print("Current Time: ", current_time)
print ("Number of minutes since midnight: ",int(t.tm_hour)*60+int(t.tm_min))
I hope you're enjoying your first steps into programming!
I believe whatever you googled was incorrect, I tried this out and also got 1336.
current_time = "22:16:15"
hrs, mins, _ = current_time.split(":")
since_midnight = int(hrs) * 60 + int(mins)
The total number of minutes in a day is 1440, so it clearly cant be more than that.
I'm trying to create a script that will print the amount of time left till I have to do a certain task.
e.g.
Biology class in 34 minutes.
I've tried
datetime.time(20, 30) - datetime.datetime.now()
but that doesn't work.
I'm not sure how to incorporate time in hours and seconds instead of days and years.
This question already has answers here:
Python: strftime, gmtime not respecting timezone
(2 answers)
Closed 3 years ago.
I am a beginner in programming and I was messing around with the time module in python, as I recently got a raspberry pi and want to start doing some projects. I imported the time module, and just made a loop to print out the time every second. The code runs correctly, but the time given is not accurate to my location. Currently, it is the 14th and a Friday, around 9 pm, but it is returning the 15th a Saturday, with 0 hours and 10 minutes. Does anyone know how I can obtain the correct time?
I tried a couple of the different functions to receive the current time like .localtime() and .gmtime() but they're all the same.
import time
while(True):
thisTime = time.gmtime()
print(time.asctime(thisTime))
time.sleep(1)
Go ahead and check out this post, I think this is your solution:
Python get current time in right timezone
So assuming your computer's time is configured correctly, you could use this:
from datetime import datetime
naive_dt = datetime.now()
It's unclear what else you've tried, although the following should work:
localtime = time.localtime()
print(time.asctime(localtime))
↳ https://docs.python.org/3/library/time.html#time.localtime