简体   繁体   中英

How can I run a simple python script hosted in the cloud on a specific schedule?

Say I have a file "main.py" and I just want it to run at 10 minute intervals, but not on my computer. The only external libraries the file uses are mysql.connector and pip requests.

Things I've tried:

PythonAnywhere - free tier is too limiting (need to connect to external DB)

AWS Lambda - Only supports up to Python 2.7, converted my code but still had issues

Google Cloud Platform + Heroku - can only find tutorials covering deploying applications, I think these could do what I'm looking for but I can't figure out how.

Thanks!

I'd start by taking a look at this question/answer that I asked previously on unix.stackexchange - I went with an AWS redhat installation and it was free to use.

Once you've decided on your VM, you can add SSH onto your server using any SSH client and upload your Python script. A personal preference is this application.

If you need to update the Python version on the server, you can do this by installing the required Python RPMs. A quick google should return the yum [or whichever RPM management system you're using] repository for the required RPMs.

Once you've installed the version of Python that you need, I'd suggest looking into the 'crontab' which can be used to schedule jobs. You can set a cronjob to run every 10minutes which will call your script.

See this site for more information on how to use the crontab

This sounds like a perfect use case for AWS Lambda which supports Python . You can invoke your Lambda on a schedule using Scheduled Events .

I see that you tried Lambda and it didn't work out for you which is too bad as that seems like the easiest route. You could also launch an EC2 instance and use userdata to schedule a cron when the instance starts.

Another option would be an Elastic Beanstalk worker with a cron.yml that defines your schedule. Elastic Beanstalk supports Python 3.4.

Update: AWS does now support Python 3.6. Just select Python 3.6 from the runtime environments when configuring.

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