简体   繁体   中英

Windows Task Scheduler job with parameter and STDOUT redirect

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

What was the answer for this? I have the same problem. I have a script that required a parameter in the task scheduler. Task Scheduler Actions tab > Program/Script: cmd. Add argument > cmd python.exe script parameter > output file >2&1. Havent have any luck running this. Is anyone knows how to do this??? Thanks in advanced.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM