I have a command line program that expects either no parameter or one parameter. If no parameter is supplied it prompts for the parameter with simple code like:
String theParameter = String.Empty;
if (args.Length == 1) theParameter = args[0];
else {
Console.Write("Please provide theParameter: ");
theParameter = Console.ReadLine();
}
Console.WriteLine("Some Output");
Interactively it works as expected:
> myprogram
Please provide theParameter:
{a value provided}
Some Output
or
> myprogram SomeValue
Some Output
or
> myprogram SomeValue > results.log
{Some Output in the results.log file)
All work as expected.
Similarly when I use the Windows 7 Task Scheduler with myprogram SomeValue
it starts, executes and completes as expected.
However when I use myprogram SomeValue > results.log
to redirect STDOUT to a file it starts, runs and never completes. If I manually run the job (by right clicking and running from the Task Scheduler) it throws up a console window with the Please provide the Parameter
.
My question is: Why is the Windows Task Scheduler job short-circuiting my parameter being passed to the program if I redirect STDOUT to a file?
Output redirection may or may not work with the Windows Task Scheduler. The workaround is to run your desired command (including output redirection) inside a batch file, and to call the batch file from Task Scheduler.
script.bat
----------
myprogram SomeValue > results.log
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With