How to make TaskScheduler fail when the task returns result different from 0

16

3

I have a backup task that fails with a non zero result, yet Task Scheduler says that it executed the task correctly.

Is there any way to tell Task Scheduler that the task failed?

Pablo Montilla

Posted 2011-09-01T13:22:54.910

Reputation: 395

See also http://stackoverflow.com/questions/16969500/how-do-i-notify-windows-task-scheduler-when-my-application-fails

– MiFreidgeim SO-stop being evil – 2016-12-01T01:48:24.573

I've submitted a suggestion to MS Feedback Hub Windows Task Scheduler does not examine the exit code. You can vote for it (link works on Win10 only)

– MiFreidgeim SO-stop being evil – 2016-12-04T12:02:11.493

Hi Pablo, We'll need more info. Like how/what is the task scheduler launching? A batch file, an EXE, something else? Is this Windows Backup that you're dealing with? What have you tried? – Ƭᴇcʜιᴇ007 – 2011-09-01T15:00:34.673

1I'm using the wbadmin program to start the backup, but I have the same problem with a batch file with a single 'exit 1' statement. The TaskScheduler includes the error code, but reports the Task as successfully ran. – Pablo Montilla – 2011-09-01T17:14:14.370

So the Task Scheduler itself shows the non-zero exit code? – Ƭᴇcʜιᴇ007 – 2011-09-01T17:19:22.117

3Yes, but reports the task as executed successfuly. I gather that maybe it's reporting that it could execute the task, not that the task itself failed, but doesn't seem very useful. – Pablo Montilla – 2011-09-02T12:22:27.020

Answers

4

If Task Scheduler successfully launches the task and the launched program exits, then from the point of view of the Task Scheduler it was a success (i.e., nothing bad happened like a failed user credential starting the program).

Normally if you want to monitor for something bad that happened during a program run you would either have the program send an alert of some sort (e.g., via email or by logging to a file that you later review), or else setup another program to monitor something about the program run that could indicate whether an error occurred.

What sort of notification of an error are you trying to get? Are you doing a manual review but just trying to have the error be more noticeable? Or do you have another program monitoring for errors?

Shannon Wagner

Posted 2011-09-01T13:22:54.910

Reputation: 983

In light of this answer, I have to ask: does Task Scheduler even trigger a retry based on exit code? – jpmc26 – 2016-06-07T03:58:44.703

2Still I think it's wrong to fail and not report (or at least give an option to report) failure of the executed task. Thanks! – Pablo Montilla – 2012-02-13T11:35:33.703

1I agree it would be useful if the Task Scheduler could detect the exit code and send an email based on the result. But I don't believe it has that feature. Are you trying to get an email alert? Maybe wrapping the launch of wbadmin in a VBScript or PowerShell script that would detect the exit code and do something with it would be a possibility for you? – Shannon Wagner – 2012-02-16T19:23:30.333

1Yup, that is a possibility. It's also work...;) Hopefully MS will think this is something useful to add to Windows 8. – Pablo Montilla – 2012-02-17T12:25:28.377

6This is mind-blowing to me - Task scheduler has all those options for configuring restart of an action in the event that it 'fails', but doesn't even check if an action returns an error code. Makes me wonder what on earth actually constitutes a 'failure' for the Task Scheduler. – mackenir – 2013-08-22T11:37:51.460

5

It is possible to "restart" the task if it fails.

  1. Use following code in the batch script to create a system error event:

    eventcreate /ID 100 /SO "Your Script Name" /L APPLICATION /T ERROR /D "Your failure reason"

  2. Create a new task with a trigger that monitors the system events and filter for the category and ID you just added in the batch. The action of this task is the batch again. This task should be delayed by one minute, so that the same batch is not started twice!

Be careful not to create an infinite loop.

Hope that helps!

schulle877

Posted 2011-09-01T13:22:54.910

Reputation: 151

I like this approach :) – DavidPostill – 2016-07-23T08:56:05.563