bat批处理循环调用程序并退出

今天测试程序时,有一个场景需要做到:启动exe程序,等待一段时间(10s)后停止该程序;再重新启动、停止,一直循环。

 

最后用批处理实现了,代码如下:

1 :a
2 start /B BlogCrawlerForIntegration.exe tvprogram_EN
3 ping 123.45.67.89 -n 1 -w 10000
4 taskkill /f /im BlogCrawlerForIntegration.exe
5 goto :a

 

代码总共5行,还是比较巧妙的。略作解释:

(1)由于bat中for循环是用起来比较麻烦,就使用goto简单的实现了无限循环;

(2)start /B BlogCrawlerForIntegration.exe tvprogram_EN

使用start命令单独启动BlogCrawlerForIntegration程序,否则批处理会挂住,一直等到BlogCrawlerForIntegration运行结束后才会往下运行;

/B 参数不让BlogCrawlerForIntegration弹出新窗口

(3)ping 123.45.67.89 -n 1 -w 10000

由于bat中没有类似sleep/wait之类的等待函数,就ping一个不存在的ip地址,超时10000毫秒,来实现sleep的效果

如果不想看到ping的输出信息,可以使用ping 123.45.67.89 -n 1 -w 10000 > null

(4)taskkill /f /im BlogCrawlerForIntegration.exe 杀掉进程,停止程序。

 

 

 

 

posted @ 2012-12-25 18:04    阅读(5741)  评论(0编辑  收藏  举报