简体   繁体   中英

What is the best way to remotely run a Python script once per day

I have a small 10 line python script that I want to run once per day at a certain time.

I want to run it at 3am so I am thinking of running it remotely.

At the moment I am looking at running an EC2 instance on AWS but it seems a little tedious for the task at hand.

Has anybody had similar experiences and if so, what was the best option for you.

I am using macOS.

You should use AWS Lambda which can be triggered using Cloudwatch, which can be scheduled using cron. Check cloudwatch Rules. You can create a rule and then add trigger to lambda based on that. AWS Doc for the same .

Another Option:

If you still want to use an EC2, then use a micro instance to perform your task, add a cron job within your EC2. Use EC2 instance scheduler for starting and stopping instances according to your need. So you'll use only EC2 when you need them. See here for more AWS Doc for Automating Time-Based Elasticity .

If the script takes under 15 minutes to run, you could:

  • Create an AWS Lambda function with your script
  • Configure an Amazon CloudWatch Event to trigger the Lambda function on a schedule

See: Creating a CloudWatch Events Rule That Triggers on a Schedule - Amazon CloudWatch Events

You can run it once adding a time.sleep(86400) in a loop. For example:

import time
while True:
    your_basic_code
    time.sleep(86400)

This is a good solution if your code is not time consuming and you are not interested for time accuracy (if you are ok with 03:01 am for example for the 10th day, etc). There are other solutions more accurate though, that will run the code exactly at 03:00

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