简体   繁体   English

python脚本失败后如何触发Windows任务计划重启

[英]How to Trigger Windows Task Schedule restart after fails with python script

I have a python script that when I find an error I throw sys.exit(1).我有一个 python 脚本,当我发现错误时,我会抛出 sys.exit(1)。 This results in the task scheduler showing a "(0x1)" comment under last run result.这导致任务调度程序在上次运行结果下显示“(0x1)”注释。 A successful run returns "The operation completed successfully. (0x0)".成功运行返回“操作成功完成。(0x0)”。 Unfortunately though this does not trigger the task to be run again even though under setting I have the "if task fails, restart every:" checkbox checked.不幸的是,尽管这不会触发任务再次运行,即使在设置下我选中了“如果任务失败,重新启动:”复选框。 Any thoughts on how to improve this?关于如何改进这一点的任何想法?

Another post has the following answer but I can not find where to put the custom filter?另一篇文章有​​以下答案,但我找不到放置自定义过滤器的位置? under the four settings;在四种设置下; Create a Basic Task, When an Event is Logged, Action,Finish创建基本任务,记录事件时,操作,完成

You can,你可以,

1 activate history for Schedule (if not already) 2 on a History "Action completed" right click "Attached Task to This Event..." 3 Set a custom filter like this: 1 激活计划的历史记录(如果还没有) 2 在历史记录“操作已完成”上右键单击“附加任务到此事件...” 3 设置自定义过滤器,如下所示:

*[System[(EventID=201)]] and *[EventData[Data[@Name='ResultCode']='1']] *[System[(EventID=201)]] 和 *[EventData[Data[@Name='ResultCode']='1']]

Given that the task scheduler's event history is enabled, you can add a trigger for each exit code for which the task should be restarted.鉴于启用了任务调度程序的事件历史记录,您可以为每个应重新启动任务的退出代码添加触发器。 Trigger "on an event" with a custom XML query.使用自定义 XML 查询“在事件上”触发。 The trigger should probably be delayed by at least 30 seconds to throttle attempts to restart the task.触发器应该至少延迟 30 秒以限制重新启动任务的尝试。

Here's an example query.这是一个示例查询。 It looks for event ID 201 (action completed) with a task named "\\PyTest" (use the full path, starting at the root "\\" folder) and an exit code of 0xC000013A (ie STATUS_CTRL_C_EXIT , ie a console process killed by Ctrl+Break).它使用名为“\\PyTest”的任务(使用完整路径,从根文件夹“\\”开始)和退出代码 0xC000013A(即STATUS_CTRL_C_EXIT ,即被 Ctrl 杀死的控制台进程)查找事件 ID 201(操作已完成) +休息)。

<QueryList>
  <Query Id="0" Path="Microsoft-Windows-TaskScheduler/Operational">
    <Select Path="Microsoft-Windows-TaskScheduler/Operational">
      *[System[EventID=201]] and 
      *[EventData[Data[@Name='TaskName']='\PyTest']] and
      *[EventData[Data[@Name='ResultCode']='0xC000013A']]
    </Select>
  </Query>
</QueryList>

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

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