Python - run script at every specific MINUTE time of the day [duplicate] - python

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())

Related

How to get the current time with python [duplicate]

This question already has answers here:
How do I get the current time?
(54 answers)
Closed 2 years ago.
How do you get the current time in python? I have seen people get the date and time, but I only want the time as a string.
For example, if the current time was 8:30, the command should output:
"8:30".
Also, how do you find what day of the week it is>
For example, if it is Tuesday hen the output should be:
2.
from datetime import datetime
print(datetime.now().strftime("%H:%M"))
Will return this:
13:29
To get seconds too:
print(datetime.now().strftime("%H:%M:%S"))
You can use
import datetime
my_time = datetime.datetime.now().time()

Receiving inaccurate time from python time module? [duplicate]

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

How can i check the time in Python [duplicate]

This question already has answers here:
Python script to do something at the same time every day [duplicate]
(4 answers)
Closed 5 years ago.
How can i check the time in Python and run a command every day at the same time?
if datetime.time() == 17.30:
bot.sendMessage(ChatID, "Message")
I found a lot in the Internet, but i didn't found something
which really helped me. I like to check the hour and the minutes.
Later it should be implimented into a TelegramBot, which should send a Message to a Group every day. So Crontab would not be so nice also a timer.
Thanks for helping
This is probably better handled with a cron job, but here's the code
import datetime.datetime
now_time = datetime.now()
If now_time.hour == 17 and now_time.minute == 30:
#do something

Using Python to get current time in yyyy-mm-ddThh:mm:ss.000Z format and also add days [duplicate]

This question already has answers here:
How to add hours to current time in python
(4 answers)
Closed 5 years ago.
I'm using Python and I need to get the time in a certain format that this API uses. So I need to get current time in the format of yyyy-mm-ddThh:mm:ss.000Z and then also create a new date in the future by adding a number of days to it.
So if I could get the current time of 2017-08-19T07:00:00.000Z and add 30 days to it to get 2017-09-18T07:00:00.000Z. What would be the easiest way to write this in Python?
Current date
import datetime
now = datetime.datetime.now()
format_iso_now = now.isoformat()
Add 10 days
from datetime import timedelta
later = now + timedelta(days=10)
format_later_iso = later.isoformat()
Output
print(format_iso_now, format_later_iso)
2017-08-20T02:43:07.177167 2017-08-30T02:43:07.177167
And to match your needs
print(now.strftime('%Y-%m-%d %H:%M:%S:%fZ'))
2017-08-20 02:48:00:103856Z
More details on documentation

Running a python script at various intervals during the day? [closed]

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.. :)

Categories