如何按计划执行python脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30835547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to execute python script on schedule?
提问by Zip
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.
Python scripts
我的机器上有两个我想在特定时间段每天执行两次。如何自动执行此任务?由于我将离开家一段时间,因此我将离开我的电脑一段时间,我想将它们上传到一个站点并从那里自动执行而无需我做任何事情。
How can I do this?
我怎样才能做到这一点?
采纳答案by SanketDG
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
如果您在 Linux 机器上,您可以使用它。Cron 是一个系统守护进程,用于在特定时间执行特定任务。
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
cron
工作原理是crontab
一个文本文件,其中包含要在指定时间运行的命令列表。它遵循特定的格式,可以在man 5 crontab
Format for crontab
crontab 的格式
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:
每个部分由一个空格分隔,最后一个部分有一个或多个空格。第 1-5 节中不允许有空格,只能在它们之间。第 1-5 部分用于指示您希望任务执行的时间和频率。这是 cron 作业的布局方式:
minute (0-59), hour (0-23, 0 = midnight), day (1-31), month (1-12), weekday (0-6, 0 = Sunday), command
分钟 (0-59)、小时 (0-23, 0 = 午夜)、日 (1-31)、月 (1-12)、工作日 (0-6, 0 = 星期日)、命令
01 04 1 1 1 /usr/bin/somedirectory/somecommand
01 04 1 1 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:
上面的示例将在 1 月 1 日凌晨 4:01 以及 1 月的每个星期一运行 /usr/bin/somedirectory/somecommand。可以使用星号 (*) 以便使用时间段的每个实例(每小时、每个工作日、每个月等)。代码:
01 04 * * * /usr/bin/somedirectory/somecommand
01 04 * * * /usr/bin/somedirectory/somecommand
The above example will run /usr/bin/somedirectory/somecommand at 4:01am on every day of every month.
上面的示例将在每个月的每天凌晨 4:01 运行 /usr/bin/somedirectory/somecommand。
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
01,31 04,05 1-15 1,6 * /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
在每年 1 月和 6 月的 1 日至 15 日的凌晨 4:00 和凌晨 5:00 之后的 01 和 31运行。
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.
上述示例中的“/usr/bin/somedirectory/somecommand”文本表示将在指定时间运行的任务。建议您使用所需命令的完整路径,如上述示例所示。which somecommand
在终端中输入以查找somecommand的完整路径。crontab 将在正确编辑和保存后立即开始运行。
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 分钟运行一次,请使用以下 crontab 条目(在可被 10 整除的分钟数上运行:0、10、20、30 等)
*/10 * * * * /usr/bin/somedirectory/somecommand
*/10 * * * * /usr/bin/somedirectory/somecommand
which is also equivalent to the more cumbersome
这也相当于更麻烦
0,10,20,30,40,50 * * * * /usr/bin/somedirectory/somecommand
0,10,20,30,40,50 * * * * /usr/bin/somedirectory/somecommand
回答by yothenberg
If you are using OSX then launchdis the preferred way to schedule tasks. There is a OSX CLI for launchd called launchctlbut if you prefer a GUI my preferred one is launchcontrol.
如果您使用的是 OSX,那么launchd是安排任务的首选方式。有一个用于 launchd 的 OSX CLI,称为launchctl,但如果您更喜欢 GUI,我的首选是launchcontrol。
回答by abautista
In Windows I have come up with two solutions.
在 Windows 中,我提出了两种解决方案。
First option: Create a .bat file.
第一个选项:创建一个 .bat 文件。
Step 1
第1步
Create a .bat file to indicate the command you want to run and the script file that will be executed, for instance:
创建一个 .bat 文件来指示您要运行的命令和将要执行的脚本文件,例如:
start C:\Users\userX\Python.exe C:\Users\userX\PycharmProjects\Automation_tasks\create_workbook.py
Step 2
第2步
Open the Task Scheduler and click on the Task Scheduler Library to see the current tasks that are executed. Click on the Create Task option.
打开Task Scheduler,点击Task Scheduler Library,查看当前执行的任务。单击创建任务选项。
Step 3
第 3 步
In the General tab, put the name of your new task and click on the option Run whether user is logged on or not
, check the option Run with highest privileges
and make sure to setup the appropriate version of you OS (in my case I picked Windows 7, Windows Server 2008 R2
.
在“常规”选项卡中,输入新任务的名称并单击选项Run whether user is logged on or not
,选中该选项Run with highest privileges
并确保设置适当的操作系统版本(在我的情况下,我选择了Windows 7, Windows Server 2008 R2
.
Step 4
第四步
In the Actions tab, click on the New button and type in the following:
在操作选项卡中,单击新建按钮并输入以下内容:
In Program/Scriptsyou need to look up for the Powershell path that the Task Scheduler will invoke to run the .bat file. In my case, my Powershell path was:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
在程序/脚本中,您需要查找任务计划程序将调用以运行 .bat 文件的 Powershell 路径。就我而言,我的 Powershell 路径是:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
In Add arguments (optional)you need to type the path of the file that will be executed by Powershell. In my case, the path was:
在添加参数(可选)中,您需要输入将由 Powershell 执行的文件的路径。就我而言,路径是:
C:\Users\userX\Desktop\run_the_bat_file.bat
C:\Users\userX\Desktop\run_the_bat_file.bat
In Start in (optional)you need to type the path of the file but without the name of the .bat file, that is:
在Start in(可选)中,您需要输入文件的路径,但不要输入 .bat 文件的名称,即:
C:\Users\userX\Desktop\
C:\Users\userX\Desktop\
Step 5
第 5 步
Click on the Triggers tab and select how often you want to execute this task.
单击触发器选项卡并选择您希望执行此任务的频率。
Step 6
第 6 步
Lastly, test your task to see if it truly works by selecting it from the Task Scheduler Library and doing click on the Run option.
最后,通过从任务计划程序库中选择它并单击运行选项来测试您的任务以查看它是否真的有效。
Second option: Run the .py file with the Task Scheduler
第二个选项:使用任务计划程序运行 .py 文件
Step 1
第1步
Open the Task Scheduler and click on the Task Scheduler Library to see the current tasks that are executed. Click on the Create Task option.
打开Task Scheduler,点击Task Scheduler Library,查看当前执行的任务。单击创建任务选项。
Step 2
第2步
In the General tab, put the name of your new task and click on the option Run whether user is logged on or not
, check the option Run with highest privileges
and make sure to setup the appropriate version of you OS (in my case I picked Windows 7, Windows Server 2008 R2
.
在“常规”选项卡中,输入新任务的名称并单击选项Run whether user is logged on or not
,选中该选项Run with highest privileges
并确保设置适当的操作系统版本(在我的情况下,我选择了Windows 7, Windows Server 2008 R2
.
Step 3
第 3 步
In the Actions tab, click on the New button and type in the following:
在操作选项卡中,单击新建按钮并输入以下内容:
In Program/Scriptsyou need to look up for the Python.exe path that the Task Scheduler will invoke to run the python script. In my case, my Python.exe path was:
C:\Users\userX\python.exe
在程序/脚本中,您需要查找任务计划程序将调用以运行 python 脚本的 Python.exe 路径。就我而言,我的 Python.exe 路径是:
C:\Users\userX\python.exe
In Add arguments (optional)you need to onlytype the name of your python script. In my case, the path was:
在添加参数(可选)中,您只需键入 Python 脚本的名称。就我而言,路径是:
Permissions_dump.py
Permissions_dump.py
In Start in (optional)you need to type the path of the file but without the name of the python script, that is:
在Start in(可选)中,您需要输入文件的路径,但不要输入 Python 脚本的名称,即:
C:\Users\userX\PycharmProjects\1099_vendors_costs
C:\Users\userX\PycharmProjects\1099_vendors_costs
Step 4
第四步
Click on the Triggers tab and select how often you want to execute this task.
单击触发器选项卡并选择您希望执行此任务的频率。
Step 5
第 5 步
Lastly, test your task to see if it truly works by selecting it from the Task Scheduler Library and doing click on the Run option.
最后,通过从任务计划程序库中选择它并单击运行选项来测试您的任务以查看它是否真的有效。
Another option (in case you convert a .py to a .exe)
另一种选择(如果您将 .py 转换为 .exe)
If you use the library Cx_Freeze to convert a .py to a .exe and you want to use the task scheduler to automate this task then you need to follow these steps:
如果您使用库 Cx_Freeze 将 .py 转换为 .exe,并且您想使用任务调度程序自动执行此任务,则需要执行以下步骤:
Step 1
第1步
Click on Create Task and then click on the Actions tab to type in the following:
单击“创建任务”,然后单击“操作”选项卡以键入以下内容:
In Program/Scriptsyou need to look up for the C:\Windows\explorer.exe
path that the Task Scheduler will invoke to run the .exe script.
在程序/脚本中,您需要查找C:\Windows\explorer.exe
任务计划程序将调用以运行 .exe 脚本的路径。
In Add arguments (optional)you need to onlytype the name of your .exe file: CustomerPopulation.exe
在添加参数(可选)中,您只需键入 .exe 文件的名称:CustomerPopulation.exe
In Start in (optional)you need to type the path of the file but without the name of the .exe file, that is:
在Start in (optional) 中,您需要键入文件的路径但不包含 .exe 文件的名称,即:
C:\Users\userX\PycharmProjects\executables
C:\Users\userX\PycharmProjects\executables
In the General tab, make sure to have selected the Run only when user is logged on
and have unchecked the Run with the highest privileges
.
在常规选项卡中,确保已选择Run only when user is logged on
并取消选中Run with the highest privileges
.
References:
参考: