任务要求:
编写start.bat/stop.bat 启动与停止应用程序,start.bat同时需要作为守护进程运行,在检测到程序关闭之后需要主动拉起,程序需要能够多版本运行,根据目录区分。例如在a/test.exe b/test.exe 两个程序,可以同时启动,点击相应目录下start.bat,启动相应test.exe,点击stop.bat只停止相应目录下的守护程序与应用程序。
实现方式如下,考虑到bat脚本作为守护进程一直在前台,会被误关闭,所以使用vbs脚本作为启动脚本(start.vbs):
Set copy_cmd = CreateObject("Scripting.FileSystemObject")
copy_cmd.CopyFile "C:\\Windows\\system32\\cmd.exe",".\cmd.exe"
Set ws = CreateObject("Wscript.Shell")
ws.run "cmd.exe /c script\\startdwzqtrans.bat",vbhide
调用script目录下(startdwzqtrans.bat)
@echo off
set _task=test.exe
set _pwd=%cd:\=\\%
set _pwd_script=%_pwd%\\script
set _svr=%_pwd%\\test.exe
set _des=script\\start.bat
set _process=wmic process where "name='%_task%' and ExecutablePath='%_svr%'" get name