Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell batch script Timeout ERROR: Input redirection is not supported, exiting the process immediately

I need to have a timeout in powershell code where I'm running batch file, in case the batch file runs for a longer time or gets stuck. I also have a timeout in the batch script timeout 300> nul from which I seem to be getting this error and it is just skipping through the timeout and executing next lines. I do not get this error if I remove the timeout from batch script. But I need timeouts at both places, how do I resolve this ? Error- ERROR: Input redirection is not supported, exiting the process immediately.

PS Code-

$bs={
cd D:\files\ 
cmd.exe /c "mybatchfile.bat"
}
$timeoutseconds=800
$j=Start-Job -Scriptblock $bs
if(wait-Job $j -Timeout $timeoutseconds) {Receive-Job $j}
Remove-Job -force $j

batch script is something like this

cmd1
cmd2
timeout 300> nul
cmd3
like image 789
HAH Avatar asked May 16 '26 09:05

HAH


1 Answers

As should be obvious by now, the timeout command does not work, because it was developed within a mere 30 years. Maybe Microsoft will get it right after a couple of more decades.

In the mean time, everyone has been using, is using, and will be using, the following command:

    ping -n 2 -w 1000 localhost > nul

This command:

  • pings localhost two times.
  • waits 1000 milliseconds between the two pings.
  • swallows any messages produced by ping.

The two pings are necessary, because if you were to ping just once, the command would end instantaneously. Still, each ping to localhost is practically instantaneous, so the command will be running for roughly 1000 milliseconds.

like image 141
Mike Nakis Avatar answered May 19 '26 03:05

Mike Nakis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!