プロセスの停止を監視する

target変数に関ししたいプロセスのexe名を記載する。ダブルクォートで囲ってはダメ

@echo off

set target=postgres.exe
set bit=0
:before

	rem timeout check
	set /a bit=%bit% + 1
	if "%bit%" == "5" (
		goto next
	)

	rem postgres.exeを探す
	for /f %%a in ('tasklist /fi "imagename eq %target%"') do set x=%%a
	echo %x% | find "情報:" > NUL
	if not ERRORLEVEL 1 (
		echo "見つからない(情報)"
		goto next
	) else (
		echo %x% | find "見つかりませんでした" > NUL
		if not ERRORLEVEL 1 (
			echo "見つからない(エラー)"
			goto next
		) else (
			echo " found %target%"
		rem 強制停止
			taskkill /im %target% /F
			timeout /t 2 > NUL
			goto before
		)
	)



:next

rem "最終的に停止したのかを確認する"
for /f %%a in ('tasklist /fi "imagename eq %target%"') do set x=%%a
if "%x%" == "%target%" (
	EXIT /B 1
) else (
	EXIT /B 0
)

2015/4/6
最終更新:2015年04月07日 01:24