简体   繁体   English

如何安排生产 python 脚本以触发 function 在每天的指定时间运行

[英]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.

相关问题 如何安排脚本每天运行 Python - How to schedule a script to run every day for Python 如何安排python脚本在特定时间每天运行? (Windows计划除外) - How can I Schedule python script to run every day at specific time ? (except Windows schedule) 如何安排 Python 脚本在每天的特定时间运行,并使其成为 AWS Elastic Beanstalk 应用程序 zip 文件的一部分? - How can I schedule a Python script to run at a particular time every day, and make it a part of AWS Elastic Beanstalk application zip file? Python:如何自动化脚本在特定时间每天运行? - Python: How to automate script to run every day at specific time? 如何安排 Python 代码每天运行多次,仅使用 python 代码 - How to schedule Python Code Run Multiple Time Every Day, With using only python code 如何安排代码在每天的特定时间运行? - How to schedule code to run at a specific time every day? 如何每天在特定时间运行python函数 - How to run a python function every day at specific time 如何在python每天特定时间运行一个function? - How to run a function every day at a specific time in python? 如何安排Python脚本在特定时间运行? - How to schedule a Python script to run at a certain time? 如何在 GCP 中每天运行 Python 脚本? - How to run a Python script every day in GCP?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM