Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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). This results in the task scheduler showing a "(0x1)" comment under last run result. A successful run returns "The operation completed successfully. (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:

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

like image 224
GoBlue_MathMan Avatar asked Oct 19 '25 10:10

GoBlue_MathMan


1 Answers

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. The trigger should probably be delayed by at least 30 seconds to throttle attempts to restart the task.

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 (i.e. STATUS_CTRL_C_EXIT, i.e. a console process killed by Ctrl+Break).

<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>
like image 87
Eryk Sun Avatar answered Oct 22 '25 01:10

Eryk Sun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!