如何按期执行python脚本?

时间:2022-05-23 01:13:56

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脚本,我想在特定的时间段执行两次。我如何自动化这个任务?因为我要离开家,也就是我的电脑一段时间,我想把它们上传到一个网站上,然后在那里自动执行,而我什么都不做。

How can I do this?

我该怎么做呢?

3 个解决方案

#1


9  

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.

如果您在Linux机器上,您可以使用cron进行此操作。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

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 004 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运行/usr/bin/somedirectory/somecommand,并在1月的每个周一运行。可以使用星号(*),以便使用一个时间段的每个实例(每个小时、每个工作日、每个月等等)。代码:

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、01 -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月1日和6月15日凌晨4:00和5:00。

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”文本指示将在指定时间运行的任务。建议您使用所需命令的完整路径,如上面的示例所示。输入终端中的哪个命令以找到该命令的完整路径。只要正确编辑和保存,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。

#2


2  

In Windows I have come up with two solutions.

在Windows中,我提出了两种解决方案。

First option: Create a .bat file.

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.

打开任务调度器并单击任务调度器库以查看正在执行的当前任务。单击Create Task选项。

如何按期执行python脚本?

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.

General选项卡,把您的新任务的名称,单击选项运行用户是否登录,查看选择使用最高权限运行,确保设置适当的版本的操作系统(在我的情况下我选择Windows 7,Windows Server 2008 R2。

如何按期执行python脚本?

Step 4

步骤4

In the Actions tab, click on the New button and type in the following:

在Actions选项卡中,单击New按钮,输入以下内容:

In Program/Scripts you 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

在程序/脚本中,您需要查找任务调度程序将调用的Powershell路径来运行.bat文件。就我而言,我的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:

在Add parameters(可选)中,您需要键入将由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:

在开始(可选)中,您需要输入文件的路径,但是没有.bat文件的名称,即:

C:\Users\userX\Desktop\

C:\Users\userX\Desktop\

如何按期执行python脚本?

Step 5

步骤5

Click on the Triggers tab and select how often you want to execute this task.

单击“触发器”选项卡,选择执行此任务的频率。

如何按期执行python脚本?

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.

最后,通过从任务调度器库中选择任务并单击Run选项,测试任务是否正常工作。

如何按期执行python脚本?

Second option: Run the .py file with the Task Scheduler

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.

打开任务调度器并单击任务调度器库以查看正在执行的当前任务。单击Create Task选项。

如何按期执行python脚本?

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.

General选项卡,把您的新任务的名称,单击选项运行用户是否登录,查看选择使用最高权限运行,确保设置适当的版本的操作系统(在我的情况下我选择Windows 7,Windows Server 2008 R2。

如何按期执行python脚本?

Step 3

步骤3

In the Actions tab, click on the New button and type in the following:

在Actions选项卡中,单击New按钮,输入以下内容:

In Program/Scripts you 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路径。就我而言,我的巨蟒。exe路径是:C:\Users\userX\python.exe

In Add arguments (optional) you need to only type 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:

在(可选)开始时,您需要输入文件的路径,但是没有python脚本的名称,即:

C:\Users\userX\PycharmProjects\1099_vendors_costs

_vendors_costs C:\Users\userX\ PycharmProjects \ 1099

如何按期执行python脚本?

Step 4

步骤4

Click on the Triggers tab and select how often you want to execute this task.

单击“触发器”选项卡,选择执行此任务的频率。

如何按期执行python脚本?

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.

最后,通过从任务调度器库中选择任务并单击Run选项,测试任务是否正常工作。

如何按期执行python脚本?

References:

引用:

  1. https://gis.stackexchange.com/questions/140110/running-python-script-in-task-scheduler-script-will-not-run?newreg=603bcdbc381b41a283e5d8d0561b835e
  2. https://gis.stackexchange.com/questions/140110/running-python-script-in-task-scheduler-script-will-not-run?newreg=603bcdbc381b41a283e5d8d0561b835e
  3. https://www.youtube.com/watch?v=oJ4nktysxnE
  4. https://www.youtube.com/watch?v=oJ4nktysxnE
  5. https://www.youtube.com/watch?v=n2Cr_YRQk7o
  6. https://www.youtube.com/watch?v=n2Cr_YRQk7o

#3


1  

If you are using OSX then launchd is the preferred way to schedule tasks. There is a OSX CLI for launchd called launchctl but if you prefer a GUI my preferred one is launchcontrol.

如果您正在使用OSX,那么launchd是安排任务的首选方式。launchd有一个OSX CLI,称为launchctl,但是如果您喜欢GUI,我更喜欢launchcontrol。

#1


9  

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.

如果您在Linux机器上,您可以使用cron进行此操作。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

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 004 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运行/usr/bin/somedirectory/somecommand,并在1月的每个周一运行。可以使用星号(*),以便使用一个时间段的每个实例(每个小时、每个工作日、每个月等等)。代码:

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、01 -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月1日和6月15日凌晨4:00和5:00。

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”文本指示将在指定时间运行的任务。建议您使用所需命令的完整路径,如上面的示例所示。输入终端中的哪个命令以找到该命令的完整路径。只要正确编辑和保存,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。

#2


2  

In Windows I have come up with two solutions.

在Windows中,我提出了两种解决方案。

First option: Create a .bat file.

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.

打开任务调度器并单击任务调度器库以查看正在执行的当前任务。单击Create Task选项。

如何按期执行python脚本?

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.

General选项卡,把您的新任务的名称,单击选项运行用户是否登录,查看选择使用最高权限运行,确保设置适当的版本的操作系统(在我的情况下我选择Windows 7,Windows Server 2008 R2。

如何按期执行python脚本?

Step 4

步骤4

In the Actions tab, click on the New button and type in the following:

在Actions选项卡中,单击New按钮,输入以下内容:

In Program/Scripts you 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

在程序/脚本中,您需要查找任务调度程序将调用的Powershell路径来运行.bat文件。就我而言,我的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:

在Add parameters(可选)中,您需要键入将由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:

在开始(可选)中,您需要输入文件的路径,但是没有.bat文件的名称,即:

C:\Users\userX\Desktop\

C:\Users\userX\Desktop\

如何按期执行python脚本?

Step 5

步骤5

Click on the Triggers tab and select how often you want to execute this task.

单击“触发器”选项卡,选择执行此任务的频率。

如何按期执行python脚本?

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.

最后,通过从任务调度器库中选择任务并单击Run选项,测试任务是否正常工作。

如何按期执行python脚本?

Second option: Run the .py file with the Task Scheduler

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.

打开任务调度器并单击任务调度器库以查看正在执行的当前任务。单击Create Task选项。

如何按期执行python脚本?

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.

General选项卡,把您的新任务的名称,单击选项运行用户是否登录,查看选择使用最高权限运行,确保设置适当的版本的操作系统(在我的情况下我选择Windows 7,Windows Server 2008 R2。

如何按期执行python脚本?

Step 3

步骤3

In the Actions tab, click on the New button and type in the following:

在Actions选项卡中,单击New按钮,输入以下内容:

In Program/Scripts you 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路径。就我而言,我的巨蟒。exe路径是:C:\Users\userX\python.exe

In Add arguments (optional) you need to only type 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:

在(可选)开始时,您需要输入文件的路径,但是没有python脚本的名称,即:

C:\Users\userX\PycharmProjects\1099_vendors_costs

_vendors_costs C:\Users\userX\ PycharmProjects \ 1099

如何按期执行python脚本?

Step 4

步骤4

Click on the Triggers tab and select how often you want to execute this task.

单击“触发器”选项卡,选择执行此任务的频率。

如何按期执行python脚本?

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.

最后,通过从任务调度器库中选择任务并单击Run选项,测试任务是否正常工作。

如何按期执行python脚本?

References:

引用:

  1. https://gis.stackexchange.com/questions/140110/running-python-script-in-task-scheduler-script-will-not-run?newreg=603bcdbc381b41a283e5d8d0561b835e
  2. https://gis.stackexchange.com/questions/140110/running-python-script-in-task-scheduler-script-will-not-run?newreg=603bcdbc381b41a283e5d8d0561b835e
  3. https://www.youtube.com/watch?v=oJ4nktysxnE
  4. https://www.youtube.com/watch?v=oJ4nktysxnE
  5. https://www.youtube.com/watch?v=n2Cr_YRQk7o
  6. https://www.youtube.com/watch?v=n2Cr_YRQk7o

#3


1  

If you are using OSX then launchd is the preferred way to schedule tasks. There is a OSX CLI for launchd called launchctl but if you prefer a GUI my preferred one is launchcontrol.

如果您正在使用OSX,那么launchd是安排任务的首选方式。launchd有一个OSX CLI,称为launchctl,但是如果您喜欢GUI,我更喜欢launchcontrol。