在虚拟机(Google云平台)中使用Cron定期运行Python脚本 [英] Running Python script at Regular intervals using Cron in Virtual Machine (Google Cloud Platform)

查看:68
本文介绍了在虚拟机(Google云平台)中使用Cron定期运行Python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以向我提供有关如何在虚拟机(使用Google Cloud Platform)内的Cron中定期执行Python脚本的步骤吗?

Can anyone provide me with the steps of how to execute a Python script at regular intervals in Cron inside Virtual Machine (using Google Cloud Platform)?

我阅读了此链接 https://cloud.google.com/appengine/docs/standard/python/config/cron 但仍然不知道如何使它工作.

I read this link https://cloud.google.com/appengine/docs/standard/python/config/cron but still could not figure out how to get it to work.

关于步骤(1)-在应用程序的根目录中(与app.yaml一起)创建cron.yaml文件.-->这意味着我们必须同时创建文件cron.yaml和app.yaml?我没有看到这些文件.app.yaml包含什么?

In regards to step (1) - Create the cron.yaml file in the root directory of your application (alongside app.yaml).--> does that mean we have to create both files cron.yaml and app.yaml? I do not see those files. What does app.yaml contain?

推荐答案

如果您按照建议使用虚拟机,则您链接的那些说明可能与App Engine无关.

If you are using a virtual machine as you suggest, then those instructions you've linked may not be relevant as they are for App Engine.

对于Compute Engine VM,您应该使用内置的Linux cron功能.对于这些说明,我假设您要每10分钟执行一次脚本.您可以根据需要调整此值.

With a Compute Engine VM you should use the inbuilt Linux cron functionality. For these instructions, I'm going to assume that you want to execute the script every 10 minutes. You can adapt this value for your needs.

如果要通过GCP虚拟机上的cron作业执行脚本,则应按以下步骤进行操作.

Here is how you should proceed if you want to execute a script via a cron job on GCP virtual machine.

1)运行此命令进入crontab配置页面.

1) Run this command to enter the crontab configuration page.

crontab -e

**请注意,上述命令将允许您编辑登录用户的crontab配置.如果要以root用户身份执行脚本,请在命令开头添加"sudo"以编辑root用户的crontab配置.

**Note, the above command will allow you to edit the crontab configuration for the user you are logged in as. If you would like to execute the script as the root user, add 'sudo' to the start of the command to edit the crontab configuration for the root user.

2)在cron配置中,您将能够添加一个以分钟,小时,一个月中的某几天,一个月和一周中的某天为间隔的条目.在同一行上,您可以添加要执行的命令-在这种情况下,可以添加执行python脚本的命令.

2) In the cron configuration, you will be able to add an entry for intervals in minutes, hours, days of month, month and day of the week. On the same line, you can add the command you would like to execute- in your case a command to execute your python script.

例如,如果您想使用python每10分钟运行一次脚本,则可以添加如下所示的条目:

As an example, if you wanted to run the script every 10 minutes with python, you would add an entry such as this:

*/10 * * * *   /usr/bin/python /path/to/you/python/script.py

3)保存crontab配置并退出文件后,需要重新启动cron服务以使更改生效.您可以通过运行以下命令来执行此操作.

3) Once you've saved the crontab configuration and exited from the file, you need to restart the cron service for your changes to take affect. You can do this by running the following command.

sudo systemctl restart cron

如果您想了解有关跑步的更多信息,请此处Linux中的cron作业.

There is some useful information here if you would like to discover more about running cron jobs in Linux.

这篇关于在虚拟机(Google云平台)中使用Cron定期运行Python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆