Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

timeout command not working in batch files

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.

I am invoking batch as: Runtime.getRuntime().exec("test.bat");

like image 263
pdg Avatar asked Dec 11 '22 14:12

pdg


1 Answers

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.

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 

When the input stream is redirected, timeout fails.

One workaround could be

( timeout /t 10 || >nul ping -n 11 localhost ) 2>nul

If the timeout fails, a ping command is executed to force the wait.

like image 86
MC ND Avatar answered Dec 29 '22 00:12

MC ND