APScheduler not starting?

2020-03-01 07:08发布

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

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)

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?

5条回答
够拽才男人
2楼-- · 2020-03-01 07:40

If you use version 2.1.0, you can also pass standalone=True parameter to the Scheduler constructor. Detail documents can be found here

from apscheduler.scheduler import Scheduler
from datetime import datetime, timedelta, time, date

def myScript():
    print "ok"

if __name__ == '__main__':
    sched = Scheduler(standalone=True)
    startDate = datetime.combine(date.today() + timedelta(days=1),time(1))
    sched.add_interval_job(myScript, start_date = startDate, days=1)
    sched.start()
查看更多
神经病院院长
3楼-- · 2020-03-01 07:47

here is my way:

from apscheduler.scheduler import Scheduler
def mainjob():
    print("It works!")

if __name__ == '__main__':
    sched = Scheduler()
    sched.start()
    sched.add_interval_job(mainjob,minutes=1)
    input("Press enter to exit.")
    sched.shutdown()
查看更多
不美不萌又怎样
4楼-- · 2020-03-01 07:50

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.

查看更多
来,给爷笑一个
5楼-- · 2020-03-01 07:55

The correct solution would be to tell the scheduler to not run as a daemon:

sched = Scheduler()
sched.daemonic = False

or

sched = Scheduler()
sched.configure({'apscheduler.daemonic': False})
查看更多
迷人小祖宗
6楼-- · 2020-03-01 07:57

I have apscheduler v3 installed and this is what I would do.

from apscheduler.schedulers.background import BackgroundScheduler  
def mainjob():
    print("It works!")

if __name__ == '__main__':
    sched = BackgroundScheduler()
    sched.start()
    sched.add_job(mainjob, 'interval', seconds=120)
    input("Press enter to exit.")
    sched.shutdown() 
查看更多
登录 后发表回答