简体   繁体   中英

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). 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.

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? If not, then it's easier to set up a cron job to call your script when needed. 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())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM