问题描述
我正在尝试通过tomcat应用程序调用批处理文件.批处理已正确执行,但批处理中的timeout命令不会暂停该批处理.直接执行批处理时,超时确实会按预期暂停该过程.
I am trying to invoke a batch file through a tomcat application. The batch is executed properly, but the timeout command in the batch does not pause the batch. When a batch is executed directly, the timeout does pause the process as expected.
我以以下方式调用批处理:Runtime.getRuntime().exec("test.bat");
I am invoking batch as:Runtime.getRuntime().exec("test.bat");
推荐答案
调用Runtime.getRuntime().exec()
时,将重定向已启动进程的输入和输出流.从此过程开始的timeout
继承相同的句柄.
When you call Runtime.getRuntime().exec()
the input and output streams of the started process are redirected. timeout
being started from this process inherits the same handles.
问题是timeout
尝试获取对控制台的访问权限,由于该操作无法执行,重定向.
The problem is that timeout
tries to get access to the console, something it can not do because of the redirection.
您可以从控制台运行此行为
You can test this behaviour running from a console
<nul timeout /t 10
重定向输入流时,timeout
失败.
When the input stream is redirected, timeout
fails.
一种解决方法可能是
( timeout /t 10 || >nul ping -n 11 localhost ) 2>nul
如果timeout
失败,则执行ping
命令以强制等待.
If the timeout
fails, a ping
command is executed to force the wait.
这篇关于超时命令不适用于批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!