Is it possible to make the script drop everything it's been doing if a timer hits a certain time?
Let's say I want it to run for 24 hours and after that it should print a few things and then exit.
Is it possible to make the script drop everything it's been doing if a timer hits a certain time?
Let's say I want it to run for 24 hours and after that it should print a few things and then exit.
This is a pretty rude method but it will work. REMEMBER: put your code after the else statement or it won't work. Also, modify the comment above exit() as the comment said. PS.: this are built-in modules. No need to install anything.
import datetime
from sys import exit
starting_time = datetime.datetime.now()
while True:
actual_datetime = datetime.datetime.now()
if actual_datetime > starting_time + datetime.timedelta(days=1):
# do what you need to do before it shut down
exit()
else:
# RUN YOUR PROGRAM HERE