Automatic restart of non responding program via CMD

  • 8 months ago
Comment

ChatGPT
1 / 1

You can use a batch script to automatically restart a non-responding program using the taskkill and start commands. Here's an example batch script that you can use to achieve this:

batch
@echo off

:restart
tasklist | find "program.exe" > nul
if errorlevel 1 (
echo Program is not running. Starting...
start "" "C:\path\to\program.exe"
) else (
echo Program is running. Killing...
taskkill /f /im program.exe
timeout /t 5 > nul
goto restart
)

echo Done.

Replace "program.exe" with the actual name of the program you want to monitor and restart. Also, replace "C:\path\to\program.exe" with the full path to the program executable.

Here's what the script does:

  1. It checks if the program is currently running using the tasklist command and filters the output with find.
  2. If the program is not running (errorlevel is 1), it starts the program using the start command.
  3. If the program is running, it forcefully terminates it using the taskkill command with the /f flag.
  4. After terminating, it waits for 5 seconds using the timeout command to give the program some time to clean up.
  5. The script then loops back to the :restart label to continue monitoring and restarting the program.

Save the script with a .bat extension and run it when needed. Keep in mind that forcefully terminating a program might lead to data loss or unexpected behavior, so use this approach with caution.