简体   繁体   English

在服务器上连续运行Python脚本

[英]Running a Python Script Continuously on Server

PREFACE To be completely honest, I'm new to this and am a bit overwhelmed with the number of options there seem to be in going about solving this. 前言坦白地说,我对此并不陌生,但是解决这个问题似乎有很多选择。

I am looking to run a Python script on a server. 我正在寻找在服务器上运行Python脚本的方法。 The code is already written and involves a handful of libraries, and runs perfectly on my computer; 该代码已经编写,并且包含少量库,并且可以在我的计算机上完美运行; but I want to be able to put it on a VM and have it run every 15 minutes while saving the data to a MySQL database. 但是我希望能够将其放在VM上并每15分钟运行一次,同时将数据保存到MySQL数据库。

Currently, the script isn't going to be accepting inputs since it is checking for changes in data via an API, but I would like to have the ability to accept inputs to the script in the future. 目前,该脚本将不接受输入,因为它正在通过API检查数据更改,但是我希望将来能够接受该脚本的输入。

I know there are a few options which let you run Python on a server and the two that I have looked into mostly are Digital Ocean and Google Compute Engine. 我知道有一些选项可以让您在服务器上运行Python,而我主要研究的两个选项是Digital Ocean和Google Compute Engine。 On Digital Ocean I started a droplet with django pre-installed and ran into problems with that approach and then I switched over to using Ubuntu through Digital Ocean. 在Digital Ocean上,我开始使用预先安装的django发行了一个版本,并遇到了这种方法的问题,然后我通过Digital Ocean切换到使用Ubuntu。

I understand that you run the start command through the console, but I still don't understand how you're suppose to upload your script to the server to begin with. 我了解您是通过控制台运行start命令的,但是我仍然不了解您应该如何将脚本上载到服务器。 I have seen ssh/scp used, but I don't understand how that uploads the libraries to the sever as well. 我已经看到使用ssh / scp了,但是我不明白如何将库也上传到服务器。

My questions are: 我的问题是:
- What is the best service to use (DO, Google, etc.)? -最好使用哪种服务(DO,Google等)?
- Is Ubuntu the right approach or is django? -Ubuntu是正确的方法还是Django?
- Is there a basic example that someone could point me in the direction of? -是否有一个基本的例子,有人可以指出我的方向?

Thank you for your time and I appreciate any help that you have to offer, some of the tutorials online just seem a little too robust for what I need. 感谢您的宝贵时间,我感谢您提供的任何帮助,一些在线教程对于我需要的功能似乎过于强大。

What I am understanding from your question is that you want a python script to run every 15 min which does some db operation and has future scope of input parametrs. 我从您的问题中了解的是,您希望每15分钟运行一次python脚本,该脚本会执行一些db操作,并具有将来的输入参数范围。

https://en.wikipedia.org/wiki/Cron https://en.wikipedia.org/wiki/Cron

Above will serve you well for this kind of need. 以上将为您满足这种需求。

You can upload your script from local system to server via scp 您可以通过scp将脚本从本地系统上传到服务器

scp filepath/filename.py user@ip:address:of:server:~/filepath/

Then login to server using ssh 然后使用ssh登录到服务器

ssh user@ip:address:of:server

Use cron to run your script every 15 minutes 使用cron每15分钟运行一次脚本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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