简体   繁体   English

带有参数和 STDOUT 重定向的 Windows 任务计划程序作业

[英]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.同样,当我将 Windows 7 任务计划程序与myprogram SomeValue一起使用时,它会按预期启动、执行和完成。

However when I use myprogram SomeValue > results.log to redirect STDOUT to a file it starts, runs and never completes.但是,当我使用myprogram SomeValue > results.log将 STDOUT 重定向到一个文件时,它会启动、运行并且永远不会完成。 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 .如果我手动运行该作业(通过右键单击并从 Task Scheduler 运行),它会弹出一个控制台窗口,其中显示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?我的问题是:如果我将 STDOUT 重定向到文件,为什么 Windows 任务计划程序作业会短路我传递给程序的参数?

Output redirection may or may not work with the Windows Task Scheduler .输出重定向可能会也可能不会与 Windows 任务计划程序一起使用 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. 任务计划程序的“操作”选项卡>“程序/脚本:cmd”。 Add argument > cmd python.exe script parameter > output file >2&1. 添加参数> cmd python.exe脚本参数>输出文件> 2&1。 Havent have any luck running this. Havent运气不错。 Is anyone knows how to do this??? 有谁知道该怎么做??? Thanks in advanced. 提前致谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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