Capture the output from a Scheduled Task

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 26, 2009

15 Comments

Philip Patrick on February 4, 2009 at 5:15 am.

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 :)

Stephen on June 21, 2009 at 10:04 am.

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

mr_snrub on July 15, 2009 at 6:55 am.

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

Anouar on February 3, 2011 at 12:48 pm.

Thanks for sharing this, it helps a lot.

Ahamed on August 10, 2011 at 1:05 pm.

Hi Rich,I have four scheuled jobs .I want to run one after another one.After first one success second one should start.Could you pls help on this.

Richard on August 10, 2011 at 8:57 pm.

@Ahamed: use a batch file.

ahamed on August 12, 2011 at 1:32 pm.

Richard.Can you pls give me some direction on this.I am new on this.Could you pls send similar batch files.So that i will try to modify and use it

usman on August 16, 2011 at 8:44 am.

hi,

i cannot see any redirected output file. i am not using a .exe file rather calling a script in scheduled tasks. please help me out

Jim on December 5, 2011 at 6:39 pm.

@Ahamed: search Google for “dos batch file examples” for examples.

TimW on July 20, 2012 at 12:12 am.

Thanks for the share fellow kiwi…exactly what I needed

Richard R on December 6, 2012 at 2:39 pm.

Thank you very much – I have been faffing about for two weeks trying to figure out why my program is failing when called from a scheduled job. I still don’t quite understand why – but I now know *how* – which is a vital first step!

Kalyan on February 28, 2013 at 8:22 pm.

Thank you. Its helped me.

gyzpunk on April 25, 2013 at 10:12 am.

Thank’s a lot, it really helps me !!!!!
What a strange thing not being able to directly redirect outputs like : myapp.exe >stdout.txt 2>stderr.txt !

KC on July 29, 2013 at 4:05 pm.

That sir is a thing of beauty, many thanks!!

Sam on June 18, 2014 at 6:02 am.

Brillo! Thanks for this. Oh why can’t Windoze just have crontab?!

Leave a Reply