windows下利用批处理脚本监控程序

1.要监控的程序为使用cygwin环境编译的exe可执行文件hello.exe,源码如下:

#include <stdio.h>

#include <unistd.h>

void main(void)

{

  while(1)

  {

    printf("hello ");

    sleep(1);

  }

}

2.以批处理形式启动hello.exe,那么启动hello.exe的批处理脚本如下:

@echo off

C:
chdir C:cygwin64in
bash --login -i -c  "/cygdrive/c/Users/jello/Desktop/hello.exe"
pause

3.监控程序hello.exe的批处理脚本如下:

:RESTART
tasklist /FI "username eq jello" | find /C "hello.exe" > temp.txt
set /p num= < temp.txt
del /F temp.txt
echo %num%
if "%num%" == "0"        start /D "C:UsersjelloDesktop" start_hello.bat
ping -n 10 -w 2000 0.0.0.1 > temp.txt
del /F temp.txt

goto RESTART