如何按计划执行python脚本? [英] How to execute python script on schedule?
问题描述
我的机器上有两个Python scripts
,我想在特定时间段每天执行两次.如何自动执行此任务?由于我将离开家,因此会离开我的计算机一段时间,因此我想将它们上传到站点并从那里自动执行,而无需执行任何操作.
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月和6月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
这篇关于如何按计划执行python脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!