如何按计划执行脚本? [英] How to execute script on schedule?

查看:23
本文介绍了如何按计划执行脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的机器上有两个 Python 脚本,我想在特定时间段内每天执行两次.如何自动执行此任务?由于我将离开家一段时间,因此我将离开我的电脑一段时间,我想将它们上传到一个站点并从那里自动执行,而无需我做任何事情.

I have two Python scripts on my machine that I want to execute two times a day on specific time period. How do I automate this task? Since I will be away from home and thus my computer for a while, I want to upload them to a site and be executed from there automatic without me doing anything.

我该怎么做?

推荐答案

如果你在 Linux 机器上,你可以使用 cron 来解决这个问题.Cron 是一个系统守护进程,用于在特定时间执行特定任务.

You can use cron for this if you are on a Linux machine. Cron is a system daemon used to execute specific tasks at specific times.

cron 的工作原理是 crontab,这是一个文本文件,其中包含要在指定时间运行的命令列表.它遵循特定的格式,可以在man 5 crontab

cron works on the principle of crontab, a text file with a list of commands to be run at specified times. It follows a specific format, which can is explained in detail in man 5 crontab

每个部分由一个空格分隔,最后一个部分有一个或多个空格.第 1-5 节中不允许有空格,只能在它们之间.第 1-5 部分用于指示您希望任务执行的时间和频率.这是 cron 作业的布局方式:

Each of the sections is separated by a space, with the final section having one or more spaces in it. No spaces are allowed within Sections 1-5, only between them. Sections 1-5 are used to indicate when and how often you want the task to be executed. This is how a cron job is laid out:

分钟(0-59)、小时(0-23,0 = 午夜)、日(1-31)、月(1-12)、工作日(0-6,0 = 星期日)、命令

minute (0-59), hour (0-23, 0 = midnight), day (1-31), month (1-12), weekday (0-6, 0 = Sunday), command

01 04 1 1 1/usr/bin/somedirectory/somecommand

以上示例将在 1 月 1 日凌晨 4:01 以及 1 月的每个星期一运行/usr/bin/somedirectory/somecommand.可以使用星号 (*),以便使用时间段的每个实例(每小时、每个工作日、每个月等).代码:

The above example will run /usr/bin/somedirectory/somecommand at 4:01am on January 1st plus every Monday in January. An asterisk (*) can be used so that every instance (every hour, every weekday, every month, etc.) of a time period is used. Code:

01 04 * * */usr/bin/somedirectory/somecommand

以上示例将在每个月的每一天的凌晨 4:01 运行/usr/bin/somedirectory/somecommand.

The above example will run /usr/bin/somedirectory/somecommand at 4:01am on every day of every month.

逗号分隔值可用于在一段时间内运行多个特定命令的实例.破折号分隔值可用于连续运行命令.代码:

Comma-separated values can be used to run more than one instance of a particular command within a time period. Dash-separated values can be used to run a command continuously. Code:

01,31 04,05 1-15 1,6 */usr/bin/somedirectory/somecommand

以上示例将在每年一月和六月的 1 日至 15 日凌晨 4:00 和凌晨 5:00 之后的 01 和 31 点运行 /usr/bin/somedirectory/somecommand.

The above example will run /usr/bin/somedirectory/somecommand at 01 and 31 past the hours of 4:00am and 5:00am on the 1st through the 15th of every January and June.

上述示例中的/usr/bin/somedirectory/somecommand"文本表示将在指定时间运行的任务.建议您使用所需命令的完整路径,如上述示例所示.在终端中输入 which somecommand 以找到 somecommand 的完整路径.一旦正确编辑和保存 crontab 就会开始运行.

The "/usr/bin/somedirectory/somecommand" text in the above examples indicates the task which will be run at the specified times. It is recommended that you use the full path to the desired commands as shown in the above examples. Enter which somecommand in the terminal to find the full path to somecommand. The crontab will begin running as soon as it is properly edited and saved.

您可能希望在每个时间单位多次运行脚本.例如,如果您想每 10 分钟运行一次,请使用以下 crontab 条目(在可被 10 整除的分钟内运行:0、10、20、30 等)

You may want to run a script some number of times per time unit. For example if you want to run it every 10 minutes use the following crontab entry (runs on minutes divisible by 10: 0, 10, 20, 30, etc.)

*/10 * * * */usr/bin/somedirectory/somecommand

也相当于比较麻烦

0,10,20,30,40,50 * * * */usr/bin/somedirectory/somecommand

这篇关于如何按计划执行脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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