[CMD] taskkill batch script 프로세스 체크 방법

728x90
반응형

 

Jenkins를 사용하다보면, cmd 및 batch script 를 이용해서 여러가지 프로세스를 제어하는 방법을 사용합니다. 

그중에 가장 많이 사용하는 것중에 하나가, 프로세스를 실행하고 죽이능 script가 아닐까 싶은데요.

물론 개인적인 의견입니다.

 

비교적 batch script 작성이 쉬운 것중에 하나가 taskkill 명령어를 이용해서 프로세스를 정지하는 것인데요.

그냥 taskkill만 사용하다 보면, 프로세스가 의도치 않게 정지된 경우, 젠킨스(Jenkins)에서 오류로 인식하는 경우가 발생합니다. 이를 해결하기 위해서 tasklist 명령어를 이용해서 사용하는 방법을 정리 하였습니다. 


 

 

 

방법 1

@echo on

tasklist /fi "imagename eq notepad.exe" |find ":" > nul
if errorlevel 1 taskkill /f /im "notepad.exe"

EXIT /B 0

 

 

방법 2

@echo off
tasklist /fi "imagename eq notepad.exe" | find /i "notepad.exe" > nul
if not errorlevel 1 (taskkill /f /im "notepad.exe") else ( echo not working process )
exit

 

 

방법 3

tasklist | find /i "notepad.exe" && taskkill /im notepad.exe /F || echo process "notepad.exe" not running.

 

 

방법 4

@echo off
set run=
tasklist /fi "imagename eq notepad.exe" | find ":" > nul
if errorlevel 1 set run=yes
if "%run%"=="yes" echo notepad is running
if "%run%"=="" echo notepad is not running
pause

 

 

 

 

참고

https://stackoverflow.com/questions/15449034/batch-program-to-to-check-if-process-exists

 

728x90