2
votes

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']]

1

1 Answers

1
votes

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>