[英]How to schedule a production python script to trigger a function to run at a specified time every Day
I am trying to schedule a python script(function) that get's data from a database (source DB) and dump them into an S3 bucket(destination DB).我正在尝试安排一个 python 脚本(函数),它从数据库(源数据库)获取数据并将它们转储到 S3 存储桶(目标数据库)中。 This would be used in production and I want a schedule that triggers this job to run at a particular time every day without human intervention.
这将在生产中使用,我想要一个触发该作业在每天特定时间运行而无需人工干预的时间表。 Please how do I go about this.
请问我如何 go 关于这个。
I have tried using this sample code below and this tends to stop if my PC goes off and maybe restart when I run the code again, this is not what I want.我已经尝试使用下面的示例代码,如果我的电脑关闭并且可能在我再次运行代码时重新启动,这往往会停止,这不是我想要的。
"""
Demonstrates how to use the blocking scheduler to schedule a job that executes on 3 second
intervals.
"""
from datetime import datetime
import os
from apscheduler.schedulers.blocking import BlockingScheduler
def tick():
print('Tick! The time is: %s' % datetime.now())
if __name__ == '__main__':
scheduler = BlockingScheduler()
scheduler.add_job(tick, 'interval', seconds=3)
print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))
try:
scheduler.start()
except (KeyboardInterrupt, SystemExit):
pass
Do you need this script to work on Windows or use less than a minute intervals?您需要此脚本在 Windows 上工作还是使用不到一分钟的时间间隔? If not, then it's easier to set up a cron job to call your script when needed.
如果没有,那么设置一个cron 作业在需要时调用您的脚本会更容易。 The script then contains only the task itself:
然后脚本只包含任务本身:
#!/usr/bin/env python3
from datetime import datetime
if __name__ == '__main__':
print('Tick! The time is: %s' % datetime.now())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.