Today I had to diagnose a problem with a Windows Scheduled Task that was sporadically failing with a non-zero return code. The exe in question was a .NET console application that was throwing an exception before Main() got called; it was outside our try-catch block.

failing-tasks

Anyway, if you ran the .exe from a command line yourself, you would see the error written to stderr. If you ran the Scheduled Task, the error was not logged anywhere.

To capture the output of the scheduled task, I redirected it to a text file with the following command:

before: NightlyBatchJob.exe

after: cmd /C NightlyBatchJob.exe >> NightlyBatchJob.output.txt 2>&1

The > symbol redirects the output to a file; >> makes it append instead of creating a new blank file each time it runs. 2>&1 makes it include the output from stderr with stdout — without it you won’t see any errors in your logs.

task

The whole command is run in a new cmd.exe instance, because just running an .exe directly from a scheduled task doesn’t seem to produce any console output at all.

January 26th, 2009 | 3 Comments
3 Responses to “Capture the output from a Scheduled Task” Leave your Comment
  1. Philip Patrick says:

    Thanks!

    Was just trying to solve the issue with “why I do not see a redirected output file at all” and found your post. After adding “cmd /c” – everything worked fine :)

  2. Stephen says:

    Thanks, was trying to figure out why this wasn’t working for me, and this solved the problem.

  3. mr_snrub says:

    Hey…TOTALLY worked for me! thank you thank you thank you!

Leave a reply

We love to hear your views.