Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows服务将输出写入文件_Windows_Batch File_Service_Output - Fatal编程技术网

Windows服务将输出写入文件

Windows服务将输出写入文件,windows,batch-file,service,output,Windows,Batch File,Service,Output,我有以下批处理脚本: echo %time% >> C:\file.txt C:\MyProgram\program.exe /arg1 >> C:\file.txt echo ------- >> C:\file.txt 我已经用创建了一个服务 sc create ProgramRun type= own start= auto binPath= "cmd /c cd /d c:\MyProgram\ && start script.bat

我有以下批处理脚本:

echo %time% >> C:\file.txt
C:\MyProgram\program.exe /arg1 >> C:\file.txt
echo ------- >> C:\file.txt
我已经用创建了一个服务

sc create ProgramRun type= own start= auto binPath= "cmd /c cd /d c:\MyProgram\ && start script.bat"
重新启动后,在
file.txt
中,我打印了正确的时间,但没有输出程序。在那个程序中,我有一些简单的
printf


如何在不编辑程序的情况下将输出写入文件?

作为Windows服务,您的程序正在本地系统帐户的上下文中运行,该帐户可能没有正确启动和运行程序所需的权限。尝试编辑该服务并将其添加到您知道可以正确执行程序的Windows帐户


此外,虽然Windows服务控制管理器(SCM)会很高兴地启动您的批处理文件,但报告的结果总是失败,特别是“错误1053:服务未及时响应启动或控制请求”。这是因为您的批处理文件无法与SCM通信!请查看Microsoft的免费(和基本)软件以避免此问题。

我想您的程序根本没有执行,因为批处理文件是作为服务执行的。如果程序的路径包含空格,请使用
“C:\MyProgram\program.exe”
。或者您的程序将消息写入
stderr
而不是
stdout
,因此您还需要
2>>C:\file.txt
。为什么不直接启动批处理文件作为服务,而不是
cmd/c cd/d c:\MyProgram\&start script.bat