Jenkins_KillProcess
@ECHO OFF
IF %1]==] GOTO Usage
FOR /F "TOKENS=1,2 DELIMS=/ " %%A IN ("%1") DO SET @DependedServer=%%A&SET @DependedProcess=%%B
IF ERRORLEVEL 1 (
ECHO ERROR: ** The input parameters - %1 init failed
GOTO ExitError
)
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
ECHO ::::: Check and kill depended process - %@DependedServer%/%@DependedProcess%
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
ECHO INFO : ## Check depended process - %@DependedServer%/%@DependedProcess% state ...
TASKLIST /S %@DependedServer% /FI "IMAGENAME EQ %@DependedProcess%"
TASKLIST /S %@DependedServer% /NH /FI "IMAGENAME EQ %@DependedProcess%" 2>NUL | FIND /I /N "%@DependedProcess%">NUL
IF ERRORLEVEL 1 (GOTO ProcessKilled)
:KillProcess
ECHO INFO : ## Kill depended process - %@DependedServer%/%@DependedProcess% ...
TASKKILL /S %@DependedServer% /FI "IMAGENAME EQ %@DependedProcess%"
IF ERRORLEVEL 1 (
ECHO ERROR: ** Could not kill depended process - %@DependedServer%/%@DependedProcess%
GOTO ExitError
)
:KillProcessLoop
::Wait for depended process to kill, sleeping for 1 second between checks
ECHO INFO : ## Waiting for depended process - %@DependedServer%/%@DependedProcess% to kill ...
PING 127.0.0.1 -n 2 > nul
TASKKILL /S %@DependedServer% /FI "IMAGENAME EQ %@DependedProcess%"
IF ERRORLEVEL 1 GOTO KillProcessLoop
::Wait an extra 2 seconds for the system to settle down so we can deploy our applications.
PING 127.0.0.1 -n 4 > nul
:ProcessKilled
ECHO INFO : ## Kill depended process - %@DependedServer%/%@DependedProcess% successfully
EXIT /B 0
:ExitError
ECHO Error : ** Kill depended service - %@DependedServer%/%@DependedProcess% failed
EXIT /B 1
:Usage
ECHO Usage: %0 DependedServer/DependedProcess
ECHO Example: %0 SZGFDEVAP11/ProcessName*