问题描述
我想在晚上运行一个 python 脚本,所以我想使用 APScheduler.我会在第二天晚上的凌晨 1 点开始运行它,它会每晚运行一次
I would like to run a python script during the night and so I was thinking of using APScheduler. I'll start running it at 1am of the following night and it will run once every night
我的调度程序脚本如下所示 (scheduler.py):
my scheduler script looks like this (scheduler.py):
from apscheduler.scheduler import Scheduler
from datetime import datetime, timedelta, time, date
def myScript():
print "ok"
if __name__ == '__main__':
sched = Scheduler()
startDate = datetime.combine(date.today() + timedelta(days=1),time(1))
sched.start()
sched.add_interval_job(myScript, start_date = startDate, days=1)
在 shell 中,我这样做:python myScheduler.py &disown
(我是远程运行的,所以我想在后台运行它并否认它.立即,该行下方出现一个数字 (PID),就像其他所有 Python 脚本一样.但是当我做 ps -e |grep python,那个数字不存在.我尝试执行 kill -9 PID
并且收到一条消息,说该作业不存在.
In the shell, I do:python myScheduler.py & disown
(I'm running it remotely, so I want to run it in the background and disown it.Immediately, a number (PID) appears below the line, as every other python script would do. But when I do ps -e | grep python, that number is not there. I tried to do kill -9 PID
and I got a message saying that the job does not exist.
调度程序是否正在运行?如果是,我该如何阻止它?如果没有,我做错了什么?
Is the scheduler running? If yes, how can I stop it? if not, what am I doing wrong?
推荐答案
你必须在 sched.add_interval_job(myScript, start_date = startDate, days=1)
之后保持脚本运行,否则脚本结束并停止.添加一个
you have to keep the script running otherwise after the sched.add_interval_job(myScript, start_date = startDate, days=1)
, the script ends and stop.add a
import time
while True:
time.sleep(10)
sched.shutdown()
之后,然后,调度程序仍然存在.
after, and then, the scheduler will still be alive.
这篇关于APScheduler 没有启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!