How to Trigger Windows Task Schedule restart after fails with python script
<p>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? </p>

<p>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</p>

<p>You can,</p>

<p>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:</p>

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

<p>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. </p>

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

<pre class="lang-xml prettyprint-override"><code>&lt;QueryList&gt;
  &lt;Query Id="0" Path="Microsoft-Windows-TaskScheduler/Operational"&gt;
    &lt;Select Path="Microsoft-Windows-TaskScheduler/Operational"&gt;
      *[System[EventID=201]] and 
      *[EventData[Data[@Name='TaskName']='\PyTest']] and
      *[EventData[Data[@Name='ResultCode']='0xC000013A']]
    &lt;/Select&gt;
  &lt;/Query&gt;
&lt;/QueryList&gt;
</code></pre>

python windows error-handling scheduled-tasks