小智 153
我花了很多时间也想在01:00启动一个简单的Python程序.出于某种原因,我无法让cron启动它,而APScheduler似乎相当复杂,应该很简单.时间表(https://pypi.python.org/pypi/schedule)似乎是正确的.
您必须安装他们的Python库:
pip install schedule
Run Code Online (Sandbox Code Playgroud)
这是从他们的示例程序修改:
import schedule
import time
def job(t):
print "I'm working...", t
return
schedule.every().day.at("01:00").do(job,'It is 01:00')
while True:
schedule.run_pending()
time.sleep(60) # wait one minute
Run Code Online (Sandbox Code Playgroud)
您需要将自己的功能放在工作位置并使用nohup运行,例如:
nohup python2.7 MyScheduledProgram.py &
Run Code Online (Sandbox Code Playgroud)
如果重新启动,请不要忘记再次启动它.
sis*_*aty 49
你可以这样做:
from datetime import datetime
from threading import Timer
x=datetime.today()
y=x.replace(day=x.day+1, hour=1, minute=0, second=0, microsecond=0)
delta_t=y-x
secs=delta_t.seconds+1
def hello_world():
print "hello world"
#...
t = Timer(secs, hello_world)
t.start()
Run Code Online (Sandbox Code Playgroud)
这将在第二天的下午1点执行一个函数(例如hello_world).
Pau*_*ood 13
APScheduler可能就是你所追求的.
from datetime import date
from apscheduler.scheduler import Scheduler
# Start the scheduler
sched = Scheduler()
sched.start()
# Define the function that is to be executed
def my_job(text):
print text
# The job will be executed on November 6th, 2009
exec_date = date(2009, 11, 6)
# Store the job in a variable in case we want to cancel it
job = sched.add_date_job(my_job, exec_date, ['text'])
# The job will be executed on November 6th, 2009 at 16:30:05
job = sched.add_date_job(my_job, datetime(2009, 11, 6, 16, 30, 5), ['text'])
Run Code Online (Sandbox Code Playgroud)
https://apscheduler.readthedocs.io/en/latest/
您可以通过将其构建到您正在安排的功能中来安排另一次运行.
小智 7
我需要类似的任务.这是我写的代码:它计算第二天并将时间更改为所需的时间,并查找currentTime和下一个计划时间之间的秒数.
import datetime as dt
def my_job():
print "hello world"
nextDay = dt.datetime.now() + dt.timedelta(days=1)
dateString = nextDay.strftime('%d-%m-%Y') + " 01-00-00"
newDate = nextDay.strptime(dateString,'%d-%m-%Y %H-%M-%S')
delay = (newDate - dt.datetime.now()).total_seconds()
Timer(delay,my_job,()).start()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
154153 次 |
最近记录: |