Anonymous
Anonymous

Reputation: 4630

How to stop currently running process in cmd without stopping the batch execution?

I have a looped batch file that runs another process.

If a process throws an error, it will basically restart itself.

Here is the short version:

:start
C:\Squid\bin\squid.exe
timeout 1
goto start

The problem with the above code is that sometimes I need to forcefully restart the currently running process.

Now if I press ctrl+c it actually terminates the batch itself and closes the window, which is not what I want........

I could have written another batch file with taskkill command, but I can't because there are many instances of this process with different process ids.

Appreciate your help!

Upvotes: 1

Views: 6043

Answers (1)

Anonymous
Anonymous

Reputation: 4630

Actually I can use start /wait command, it's the same as the above as it will wait for the file to execute but will open it in a different window, which I could easily close and force the program to restart without terminating my batch.

:start
start /wait C:\Squid\bin\squid.exe
timeout 1
goto start

Note: upon closing the batch may prompt to terminate execution. I found the way dealing with it here: https://superuser.com/questions/35698/how-to-supress-terminate-batch-job-y-n-confirmation

rem Bypass "Terminate Batch Job" prompt.
if "%~1"=="-FIXED_CTRL_C" (
   REM Remove the -FIXED_CTRL_C parameter
   SHIFT
) ELSE (
   REM Run the batch with <NUL and -FIXED_CTRL_C
   CALL <NUL %0 -FIXED_CTRL_C %*
   GOTO :EOF
)

Upvotes: 1

Related Questions