Created
May 21, 2023 13:03
-
-
Save davutg/50900ea44c684aa8addbff863c2a4295 to your computer and use it in GitHub Desktop.
windows batch script for automatically restarting a process when a certain port is not attached to any process
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ECHO OFF | |
FOR /F "tokens=7 delims=: " %%G in ('netstat -ano ^| findstr 48823') DO SET PID=%%G | |
IF [%PID%]==[] (goto kill_pid) ELSE (ECHO "A process is already running with the ID: " %PID% && goto check_status) | |
:kill_pid | |
for /f "usebackq tokens=1" %%A in (`wmic process where "CommandLine like '%%ABC_PROCESS%%'" get ProcessId`) do taskkill /f /PID %%A | |
ECHO %DATE% %TIME% ABC_PROCESS process restarted >> RESET_LOG.txt | |
goto start_new | |
goto:eof | |
:start_new | |
CD C:\dist | |
START CMD /C ABC_START.bat | |
goto:eof | |
:check_status | |
FOR /F "tokens=6 delims=: " %%T in ('netstat -ano ^| findstr 48823') DO SET NET_STAT=%%T | |
ECHO "STATUS:" %NET_STAT% | |
IF NOT "%NET_STAT%"=="ESTABLISHED" (goto kill_pid) ELSE (ECHO "everything is alright" && goto end) | |
goto:eof | |
:end | |
echo "done" | |
goto:eof |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment