1

I have this bash script looped running in Linux, what is the Windows equivalent of doing this?

while ./process.sh; do sleep 1; done

I have a windows batch file that I would like to run as long as the return code is 0

J. Chomel
  • 8,193
  • 15
  • 41
  • 69
the dave
  • 71
  • 1
  • 8

2 Answers2

5
cmd /q /e /c"for /l %%a in () do (call process.cmd || exit) & >nul timeout /t 1"

If you want to test it from command line, change %%a with %a

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • Me too! @MCND, why are you using `cmd /C`? I think it is not necessary, is it? just because of `/Q`?? – aschipfl Sep 01 '15 at 20:31
  • 1
    @aschipfl, no, it is because I don't know if the process in the caller batch file needs to continue after this line. If I run the infinite `for /l` in batch context I can only use a `exit` to leave it, but the batch file will end. Starting the infinite loop in a separate `cmd` instance (that will run code in command line context) I can use `exit` or `exit /b` to leave the loop and keep running the caller batch file. – MC ND Sep 01 '15 at 20:37
  • Aah, I see -- thanks a lot for your explanation! just note that `exit` **`/B`** **cannot** be used, it will **hang**... – aschipfl Sep 01 '15 at 21:18
  • 1
    @aschipfl, no, tested and it works as indicated. If the infinite `for /l` is running in command line context (the reason for the `cmd /c`, then the `exit /b` can force the loop to end. But, if the infinite `for /l` is executed in batch context (without the `cmd /c`) you can only leave it with an `exit` – MC ND Sep 01 '15 at 21:30
  • Oops, I confused your explanation and my past opinion... yes, `exit /B` equals `exit` unless it is executed in a batch context... (I had in mind that `for` loops left by `goto` or `exit /B` continue iterating in the background until completion, but yes, `exit` will _always_ terminate it) – aschipfl Sep 01 '15 at 21:46
  • Wow, this is very nice. It uses less CPU than some of the others I found here, thanks! – the dave Sep 02 '15 at 00:53
  • 1
    excelent... I was searhin for a loop to use in command line and this one do the job quick'n'dirty !!! THX ~~~ – ZEE Oct 17 '16 at 17:48
2

I think you are looking for something like this:

:LOOP
timeout /T 1 /NOBREAK
call "\path\to\your\process.sh\equivalent\batch\file"
if not ErrorLevel 1 goto :LOOP

Note: This does not work on the command line. You need to place this code in a batch file.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • 1
    The `timeout` must be placed _before_ the `call`; otherwise the timeout modify the errorlevel returned by the batch file. – Aacini Sep 01 '15 at 20:22
  • Thanks for the hint, @Aacini, i forgot about that... see my [edit](http://stackoverflow.com/revisions/32339498/3)... – aschipfl Sep 01 '15 at 20:26