25

I am working with Windows 7 and I have an application that returns zero (0x0) when successful and one (0x1) on error situations.

I have scheduled this app using Windows Task Scheduler. I have checked the option boxes "If the task fails, restart every" and "Attempt to restart up to:".

I thought that a non-zero return code from the app would be enough to trigger the task to be restarted after the given interval. But nothing happens.

Any ideas what could be the issue? I tried to google it but did not found anything relevant.

5 Answers5

14

Create a new task and set the custom event query like this:

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

Set the trigger advanced settings to Delay the task for a period of time like 15 minutes.

Configure the action of the new task to start a program:

Program/script:

schtasks

Add arguments:

/Run /TN "\YOUR TASK NAME HERE"

This will schedule the original task to run again 15 minutes after a non-zero result code is logged in the event.

AndreRoberge
  • 55
  • 10
dnk.nitro
  • 496
  • 6
  • 13
  • 1
    Valid and works on Windows 10. It should be noted that this solution is an additional scheduled task that is configured to monitor failed executions (non zero returns) so any script being run in the original task should send proper return codes. Remembering crucially that you should set a delay of at least one minute and that you should implement a failsafe to prevent the original script from re-running forever more. Still a great answer, kudos. – Lewis Jan 31 '16 at 20:39
  • 1
    Wait, is this just setting up a custom trigger to fire off the task again on non-zero exit code? – jpmc26 Jun 09 '16 at 17:55
6

I've experienced the same problem on a Windows 2008 server Windows Task Scheduler. The action return a non zero code but the scheduler consider the task completed:

Task Scheduler successfully completed task "\SET Tasks\Scheduled task [Backup SET Server]" , instance "{...}" , action "C:\Windows\SYSTEM32\cmd.exe" with return code 1.

I've found on the web only one answer:

The Windows Task Scheduler does not examine the exit code or any other values when your task completes. You must handle any error processing within your own script or program.

...in this document: www.onlinetoolworks.com/docs/winTaskSched.doc

So I think now that the only way to workaround this problem may be to use task triggering on event. I'm investigating.

Regards,

Olivier.

Olivier
  • 161
  • 1
  • 4
  • I did not manage to make it work with task triggering on event. – Olivier Nov 27 '09 at 15:15
  • I've submitted a suggestion to MS Feedback Hub [Windows Task Scheduler does not examine the exit code](http://aka.ms/a5p0w2). You can vote for it (link works on Win10 only) – Michael Freidgeim Dec 04 '16 at 12:00
3

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

Enjoy

Tilo

used on Win 2008 R2 (Exchange as email server)

Tilo
  • 31
  • 1
  • 1
    Hi Tilo, I am struggling with the same problem and I tried your custom filter technique but the event keeps firing. I need it to fire only every so often and only a couple of times. Could you expand a bit on the suggested solution – Johannes May 26 '11 at 13:12
0

Nobody has answered the title question though. It seems as though the task scheduler has no way to detect a failed task? Surely it must have something because it has an option to restart failed tasks!

Jason Cheng
  • 180
  • 2
  • 12
0

The option "If the task fails, restart" is misleading, a failed task is when Task Scheduler is not able to run one of the action correctly, not when the script itself runs and return an error code.

A very clever workaround suggested here can be used instead of additional task querying for events:

The issue is the actions are only considered to fail if they cannot be started. This is pretty dumb, but the scheduler doesn't care about the results of the actions.
One workaround is to add an action at the end to run something like "ok.exe" and then have your other actions either create "ok.exe" (good result) or delete it. (bad result)

This way when the Task Scheduler goes to run the last action it will fail to start it (if you had removed it, because your previous action failed). This will cause the Task Scheduler to go ahead and Queue up your Task for a restart based on the restart settings on the Scheduled Task.

PS: To create a dummy ok.exe, I usually just copy c:\windows\system32\clip.exe c:\mytask\ok.exe

-- Tolga

So what is suggested is to create a final action for the task that will call a dummy .exe file. If the .exe file is not found, this will correctly cause the failure for the task and trigger the restart option. As .exe file he uses clip.exe from Windows system folder, this is nice I think because is very small and won't do any action if called on it's own.

Caution
I've tried with empty .cmd and .bat files but this will not trigger correctly the fail trigger. It must be a .exe file apparently.

task-scheduler-dialog

Of course a mechanism to rename/move the ok.exe file in case your target script/job fails is needed.
I've pointed the Task Scheduler to use the wrapper script below with success. Edit the path to ok.exe as needed, I've copied it in the same directory as this wrapper script:

@echo off

:: This will reset the .exe name to make sure the .exe is available if error is
:: not triggered. See `:trigger_error`.
if exist ok.exe.nope (
    rename ok.exe.nope ok.exe
)

:: This script is used as batch runner by Task Scheduler.
:: MAKE SURE THE SCRIPT WILL RETURN AN ERROR CODE !=0
echo Here is my script!

:: If script above exited with an error, rename the `ok.exe` file. This will
:: cause a failure for Task Scheduler following action attempting to run
:: `ok.exe`, thus triggering the restart conditions.
:: See https://social.technet.microsoft.com/Forums/Lync/en-US/4545361c-cc1f-4505-a0a1-c2dcc094109a/restarting-scheduled-task-that-has-failed#e4e3ff74-2d42-4d58-a930-a7838a0762ff
:trigger_error
if %errorLevel% NEQ 0 (
    rename ok.exe ok.exe.nope
)
Gruber
  • 2,196
  • 5
  • 28
  • 50