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?

6
  • 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? Sep 1, 2011 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. Sep 1, 2011 at 17:14
  • So the Task Scheduler itself shows the non-zero exit code? Sep 1, 2011 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. Sep 2, 2011 at 12:22

2 Answers 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?

5
  • 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! Feb 13, 2012 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? Feb 16, 2012 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. Feb 17, 2012 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, 2013 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 7, 2016 at 3:58
6

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!

1
  • I like this approach :)
    – DavidPostill
    Jul 23, 2016 at 8:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.