19

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?

Tamara Wijsman
  • 57,083
  • 27
  • 185
  • 256
Pablo Montilla
  • 435
  • 1
  • 4
  • 15
  • 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 Sep 01 '11 at 15:00
  • 1
    I'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 Sep 01 '11 at 17:14
  • So the Task Scheduler itself shows the non-zero exit code? – Ƭᴇcʜιᴇ007 Sep 01 '11 at 17:19
  • 3
    Yes, 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 Sep 02 '11 at 12:22
  • See also http://stackoverflow.com/questions/16969500/how-do-i-notify-windows-task-scheduler-when-my-application-fails – Michael Freidgeim Dec 01 '16 at 01:48
  • 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:02

2 Answers2

7

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!

DavidPostill
  • 153,128
  • 77
  • 353
  • 394
schulle877
  • 171
  • 1
  • 2
5

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
  • 1,041
  • 1
  • 10
  • 20
  • 2
    Still 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 Feb 13 '12 at 11:35
  • 1
    I 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 Feb 16 '12 at 19:23
  • 1
    Yup, that is a possibility. It's also work...;) Hopefully MS will think this is something useful to add to Windows 8. – Pablo Montilla Feb 17 '12 at 12:25
  • 8
    This 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 Aug 22 '13 at 11:37
  • In light of this answer, I have to ask: does Task Scheduler even trigger a retry based on exit code? – jpmc26 Jun 07 '16 at 03:58