, , script.
, :
import threading
import schedule
import time
import datetime
import sys
def test():
print('{} This is a test'.format(datetime.datetime.now()))
def exit():
print('{} Now the system will exit '.format(datetime.datetime.now()))
sys.exit()
schedule.every().day.at("09:57").do(test)
schedule.every().day.at('09:58').do(exit)
while True:
schedule.run_pending()
time.sleep(1)
" ", "exit", script.
, :
def test():
while True:
print "This is a test"
time.sleep(5)
script .
, Python while test .
, , , , .
, , 10:00, 12:30. , , , .
, Python Schedule, .
" " Overflow, , Python 2.7:
import threading
import schedule
import time
import datetime
import sys
def doit(stop_event, arg):
while not stop_event.wait(1):
print ("working on %s" % arg)
print("Stopping as you wish.")
def startit():
global pill2kill
global t
pill2kill = threading.Event()
t = threading.Thread(target=doit, args=(pill2kill, "task"))
t.start()
def stopit():
global pill2kill
global t
pill2kill.set()
t.join()
schedule.every().day.at("12:48").do(startit)
schedule.every().day.at('12:49').do(stopit)
while 1:
schedule.run_pending()
time.sleep(1)
Python Crontab , .
PS: , Python Schedule Lib , script date.now() , , - (, Schedule Lib).
cron, script , datetime. /.
from datetime import datetime
import time
def test():
global hasrun
print('{} This is a test'.format(datetime.now()))
time.sleep(5)
hasrun=True
year,month,day,hour,minute=2016,12,23,15,55
hasrun=False
now=datetime.now()
print "Now the time is :", now
jobstart=datetime(year,month,day,hour,minute)
jobstop=datetime(year,month, day,hour,minute+1)
print "Job will run at: ", jobstart
print "Job will finish at: ", jobstop
while True:
while ((datetime.now() > jobstart) and (datetime.now() < jobstop )):
test()
else:
print('{} Please Wait...'.format(datetime.now()))
if hasrun:
minute=minute+2
jobstart=datetime(year,month,day,hour,minute)
jobstop=datetime(year,month, day,hour,minute+1)
print "the job will run again ", jobstart
print "and will finish at ", jobstop
hasrun=False
time.sleep(5)