Scheduling a python script with Windows task scheduler

huangapple go评论51阅读模式
英文:

Scheduling a python script with Windows task scheduler

问题

Sure, here's the translated content:

设置概述:

  • W10专业版,位于域内
  • 所有脚本和文件都位于一个文件夹中(不是本地文件夹,而是连接到文件服务器的网络驱动器)
  • 两个Python脚本:一个用于筛选Excel文件(删除列等),另一个用于编辑HTML文件并将其填充为Excel文件的内容。

这些脚本(它们工作正常,没有错误或问题,因此无需调试,但也许路径引用可能是我下面在这篇帖子/问题中要解释的主要问题):

  • 用于将一个Excel文件转换为另一个以筛选数据的脚本使用以下主要路径引用:

读取Excel:

df = pd.read_excel('H:\python codes\excel\planner software\planning-update.xls')

写入:

df.to_excel('H:\python codes\excel\planner software\planning-update-filtered.xlsx', index=False)

启动下一个用于创建/编辑HTML文件的Python脚本:

subprocess.call(["python", "H:\python codes\excel\planner software\make-website.py"])
  • 用于读取Excel,打开HTML,编辑HTML的脚本使用与上述相同的“读取”引用用于Excel,并使用此引用来打开HTML:

打开HTML:

open("H:\python codes\excel\planner software\calendar.html", "w") as f:

目标:
目标是自动化第一个脚本,并让它每天在特定时间运行。无论用户是已登录还是未登录都应该能够运行。我尝试使用Windows任务计划程序来实现这一目标。

我尝试过的:
我在任务计划程序中创建了一个任务。我选择了“无论用户登录状态如何都运行任务”的选项。在操作中,我选择了“启动程序”,将程序/脚本放在存储路径“\storage\Users\lambra\python codes\excel\planner software\planner.py”,并在“起始位置”字段中放入“\storage\Users\lambra\python codes\excel\planner software”。

我按照其他StackOverflow帖子和Google搜索结果进行了操作。我也尝试过使用驱动器字母。有时我的设置会导致任务计划程序出错,无法运行任务。然而,使用这些设置,它显示任务已经运行,但我注意到HTML文件没有更改。在任务计划程序中,我可以看到任务已启动(事件ID 100),编辑已开始(ID 200),并且我得到一行:

由用户启动的任务(ID 110)

我已经尝试将这些文件的所有权限更改为允许所有操作。手动运行Python脚本甚至使用批处理文件时,它都可以工作,但是计划运行却无法正常工作。

英文:

Overview of the setup:

  • W10 professional within a domain
  • All scripts and files within 1 folder (not local but network drives connected to the file server)
  • 2 python scripts: one to filter an Excel file (removing columns and stuff), another to edit an HTML file and fill it with the content of the excel file.

The scripts (they work perfectly, no errors or problems so no debug needed but maybe the path references are causing my main problem that I'll explain in the lower part of this post/question):

  • The script to convert 1 excel file to another to filter data uses following main path references:

Reading the excel:

 df = pd.read_excel('H:\python codes\excel\planner software\planning-update.xls')

Write it:

df.to_excel('H:\python codes\excel\planner software\planning-update-filtered.xlsx', index=False)

start the next python script that creates/edits the html file:

subprocess.call(["python", "H:\python codes\excel\planner software\make-website.py"])    
  • The script to read the excel, opens the html, edits the html uses the same "read" reference for excel as above and uses this one to open the html:

Open html:

open("H:\python codes\excel\planner software\calendar.html", "w") as f:

The goal:
The goal is to automate the first script and let it run everyday at a specific hour. It should be done if the user is logged in or logged out. It shouldn't matter. I try to do this with windows task scheduler.

What I tried:

I created a task within the task scheduler. I selected the box to run the task no matter the login status of the user. Within Action is chose for "start program", for program/script i put in the storage path "\storage\Users\lambra\python codes\excel\planner software\planner.py" and in the start in field, i put "\storage\Users\lambra\python codes\excel\planner software"

I did this following other stackoverflow posts and google findings. I tried it with drive letters as well. Sometimes my settings where setup that the task scheduler gave an error that it could not run the task. However with these settings, it says it ran the task but I notice the HTML file is not changed. In task scheduler I can see that the task is started (event ID 100), the editing is started (ID 200) and I get a line with

> task started by user (ID 110)

I already tried to change all permissions to allow everything for those files. When running the python script manually or even with a batch file, it works, but scheduling it, does not.

答案1

得分: 1

以下是翻译好的内容:

我曾经遇到过同样的问题,所以我想出了一个解决方法,因为似乎TS(任务计划程序)对Python不友好。
现在我使用任务计划程序来运行一个.bat文件,依次执行Python程序。

文件应该是这样的:

run_from_TS.bat

:: 打开存储程序的磁盘
D:
:: 打开Python脚本的项目文件夹
cd "D:/path/to/project"
:: 使用正确的Python实例打开要执行的Python脚本
"D:\path\to\project\Environment\Scripts\python.exe" "D:\path\to\script\to\run"

请注意我使用"/"和"\"。

在任务计划程序中的选项如下:

常规: 不使用最高权限运行(Run with highest privileges)已禁用。

触发器: 任何您想要的时间,但确保状态为启用。

操作: 启动程序(路径指向您的.bat文件,按照此示例并使用"": "D:\the\path\to\file.bat")。

条件: 由您选择。

设置: 由您选择。我建议启用“允许按需运行任务”。

附注:

我还建议在运行脚本之前,先熟悉一下.bat和任务计划程序如何共同工作。请记住,您可以在任务计划程序的设置中启用按需运行任务,并测试一下它是来自任务计划程序部分还是.bat部分导致问题。

附注2(我的任务计划程序配置):

Scheduling a python script with Windows task scheduler

Scheduling a python script with Windows task scheduler

Scheduling a python script with Windows task scheduler

Scheduling a python script with Windows task scheduler

Scheduling a python script with Windows task scheduler

英文:

I had this same issue once, and came up with a workaround since it seems TS is not python friendly.
Now what I use Task Scheduler to run a .bat file that executes the python programs one after the other.

The file should look like this:

run_from_TS.bat

:: Open the disk where you store your program
D:
:: Open the project folder for your python scripts
cd "D:/path/to/project"
:: Open the python script you want to execute using the correct python instance
"D:\path\to\project\Environment\Scripts\python.exe" "D:\path\to\script\to\run"

Please note that i use "/" and "".

On the TS the options are as follows:
General: Run with highest privileges is DISABLED.
Triggers: Whenever you want, but make sure Status is Enabled.
Actions: Start a program (path to your .bat file following this example and using "": "D:\the\path\to\file.bat").
Conditions: Your choice here.
Settings: Your choice here. I recommend enabling "Allow task to be run on demand".

PD:
I would also recommand you to familiarize yourself on how .bat and TS work (togheter) before running your scripts. Remember you can enable the task to be run on demand on the TS settings as mentioned above and test if it is not working from the TS part or the .bat part.

PD 2 (my TS config):
Scheduling a python script with Windows task scheduler

Scheduling a python script with Windows task scheduler

Scheduling a python script with Windows task scheduler

Scheduling a python script with Windows task scheduler

Scheduling a python script with Windows task scheduler

huangapple
  • 本文由 发表于 2023年4月19日 15:36:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76051851.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定