简体   繁体   English

通过 Windows 调度程序运行 python 脚本不起作用

[英]Running a python script through Windows Scheduler not working

I am trying to automate a python script through the Windows Task Scheduler but its not working.我正在尝试通过 Windows 任务计划程序自动化 python 脚本,但它不起作用。 At the end of my python script.在我的 python 脚本结束时。 two CSV files should be created but they arent.应该创建两个 CSV 文件,但它们不是。

I tried the following: 1. Copied the address of my python.exe to Program/Script.我尝试了以下方法: 1. 将我的 python.exe 的地址复制到 Program/Script。

C:\Program Files\Python35\python.exe C:\Program Files\Python35\python.exe

  1. In the Add arguments, i put the name of my file在添加 arguments 中,我输入了我的文件名

Historical Aggregation.py历史聚合.py

  1. In the Start in (optional), i put the path of my python script在开始(可选)中,我输入了 python 脚本的路径

C:\Users\myname\PycharmProjects\Project1 C:\Users\myname\PycharmProjects\Project1

Am I missing something我错过了什么吗

To simplify, we can create a really short .bat file, that will only receive the necessary command to run your python script. 为简化起见,我们可以创建一个非常短的.bat文件,该文件仅会收到运行python脚本所需的命令。

To do so, try this: 为此,请尝试以下操作:

Create a executePy.bat file in the same folder than your Python file (C:\\Users\\myname\\PycharmProjects\\Project1), with content: 在与Python文件(C:\\ Users \\ myname \\ PycharmProjects \\ Project1)相同的文件夹中创建executePy.bat文件,其内容为:

@echo off
"C:\Program Files\Python35\python.exe" "Historical Aggregation.py"

Then, on your task scheduler, simply schedule a test with Program/Script: 然后,在您的任务计划程序上,只需使用程序/脚本计划测试:

"C:\Users\myname\PycharmProjects\Project1\executePy.bat"

Leave Add Arguments and Start In in blank. 将“ Add Arguments和“ Start In留为空白。 Now, your task should be ready to run. 现在,您的任务应该可以运行了。

Another approach would be to set fields as: 另一种方法是将字段设置为:

  1. Program/Script - your python path (with quotation marks): 程序/脚本-您的python路径(带引号):

"C:\\Program Files\\Python35\\python.exe" “ C:\\ Program Files \\ Python35 \\ python.exe”

  1. Add arguments - full file name of the script, including it's path (with quotation marks): 添加参数-脚本的​​完整文件名,包括其路径(带引号):

"C:\\Users\\myname\\PycharmProjects\\Project1\\Historical Aggregation.py" “ C:\\ Users \\ myname \\ PycharmProjects \\ Project1 \\ Historical Aggregation.py”

I had a very similar issue, and solved it in a different way. 我有一个非常相似的问题,并以不同的方式解决了。 Here my step by step guide: 这是我的逐步指南:

  1. Transform the python script to an .exe, using in the DOS cmd prompt the command: 在DOS cmd提示符下使用以下命令将python脚本转换为.exe:

    pyinstaller -- onefile [name of the file.py]

  2. Place the CSV file that you want to update in the same folder as the .exe file created 将要更新的CSV文件与创建的.exe文件放在同一文件夹中

  3. Create a basic task on Windows Scheduler, with the following properties: 在Windows Scheduler上创建具有以下属性的基本任务:

    • General - select 一般-选择

      • Run whether user is logged on or not 运行用户是否已登录
      • add the PC password. 添加PC密码。 For my PC, use the user name DESKTOP-M40FS79\\dario and the PC password 对于我的PC,使用用户名DESKTOP-M40FS79 \\ dario和PC密码
      • Run with highest privileges 以最高特权运行
    • Triggers – select 触发器–选择

      • Daily 日常
      • Repeat task every 30 minutes 每30分钟重复一次任务
      • Stop task if it runs longer than 15 minutes 如果任务运行时间超过15分钟,请停止
    • Actions 操作

      • Under Program/ Script insert the path to your .exe file, for instance, C:\\Pythondata\\dist\\test.exe 在“程序/脚本”下,插入.exe文件的路径,例如C:\\ Pythondata \\ dist \\ test.exe
      • Under Start in (optional) insert the path to the directory where the CSV and .exe files are, for instance C:\\Pythondata\\dist\\ 在“开始于”(可选)下,插入CSV和.exe文件所在目录的路径,例如C:\\ Pythondata \\ dist \\
    • Conditions - select 条件-选择

      • Start the task only if the computer is on AC power, and make sure you have connected the power 仅在计算机使用交流电源时才启动任务,并确保已连接电源
      • Wake the computer to run this task 唤醒计算机以运行此任务
    • Settings – leave the default options 设置-保留默认选项

  4. Save the task by inserting the password 通过插入密码来保存任务

  5. Leave the task status on “Ready” 将任务状态保留为“就绪”

Good luck! 祝好运!

I have a problem with the task exiting.. It runs, but then it says "task is currently running".我的任务退出有问题。它运行,但随后显示“任务当前正在运行”。 It's a 1 second python script... should run and end.. I'm using this for code:这是一个 1 秒的 python 脚本......应该运行并结束......我正在使用它的代码:

C:\temp\python\file.bat C:\temp\python\file.bat

and it's content is:它的内容是:

@echo off C:/Users/benjune/AppData/Local/Programs/Python/Python310-32/python.exe C:\temp\python\ben\file.py @echo off C:/Users/benjune/AppData/Local/Programs/Python/Python310-32/python.exe C:\temp\python\ben\file.py

It's scheduled to run every 5 minutes, and should finish really quickly.它计划每 5 分钟运行一次,并且应该很快完成。 Should I use an exit or something after that?之后我应该使用出口还是其他什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM