Search code examples
batch-filededicated-server

.BAT file keeps opening unlimited instances after Windows Update


So this bat file was running perfectly right before this latest windows update.

@Echo off
:Start
Start E:\directoryhere?listen -dedicated
echo Press Ctrl-C if you don't want to restart automatically
ping -n 10 localhost
goto Start

So this would start a dedicated server. A command prompt would pop up. When everyone left the server or the game finished, the command prompt would close then the .bat file would reopen it. Now after this update, the .bat file just keeps opening the cmd prompt while its open. So i'll have instantly 20 instances open at once and my cpu is at 100%.

I've also had this code before the windows update before this one which ended up doing the same thing.

@echo off
cd "E:\directoryhere\"
:loop
Start RxGame-Win64-Test.exe server lv_canyon?listen -dedicated | set /P "="
goto loop

That code used to work, but 2 window updates before ended up doing the same thing. It would just keep opening instances and make my cpu 100%.

What's a way to make sure to see if the cmd prompt is open and not to reopen it and keep it running until the cmd prompt closes then reopen it.


Solution

  • A simple fix to this can to check is the process already is open first using tasklist. Please make sure you search what your actual application is called. For this example I'm going to guess it's called RxGame-Win64-Test.exe. Bellow are a few script options.

    This script bellow will check to see if the RxGame-Win64-Test.exe app is open first before starting another one:

    @ECHO OFF
    @SETLOCAL enabledelayedexpansion
    GOTO LOOP
    
    :LOOP
    
    Rem | Check If Window Is Open
    tasklist /FI "IMAGENAME eq RxGame-Win64-Test.exe" 2>NUL | find /I /N "RxGame-Win64-Test.exe">NUL
    if not "%ERRORLEVEL%"=="0" (
    
        Rem | Process Not Found
        timeout /T 10 /NOBREAK
    
        Rem | Restart Server
        start "" "RxGame-Win64-Test.exe server lv_canyon?listen -dedicated"
    
        Rem | GOTO LOOP
        GOTO LOOP
    
    )
    
    GOTO LOOP
    

    Not sure if the RxGame-Win64-Test.exe is CMD based program or not but if it is the script bellow will help you:

    @ECHO OFF
    @SETLOCAL enabledelayedexpansion
    
    Rem | First Load, Start Server
    start "DedicatedServerLauncher" cmd /c "RxGame-Win64-Test.exe server lv_canyon?listen -dedicated"
    
    GOTO LOOP
    
    :LOOP
    
    Rem | Reset PID
    Set "PID="
    
    Rem | Grab The Current Window PID
    FOR /F "tokens=2" %%# in ('tasklist /v ^| find "DedicatedServerLauncher" ^| find "Console"') do set PID=%%#
    
    Rem | Check If Window Is Open
    if "!PID!"=="" (
    
        Rem | Process Not Found
        timeout /T 10 /NOBREAK
    
        Rem | Restart Server
        start "DedicatedServerLauncher" cmd /c "RxGame-Win64-Test.exe server lv_canyon?listen -dedicated"
    
        Rem | GOTO LOOP
        GOTO LOOP
    )
    
    GOTO LOOP
    

    For help on any of the commands do the following:

    • call /?
    • set /?
    • for /?
    • if /?
    • find /?
    • So on.