批处理文件 - 关闭由批处理文件进程打开的单独cmd窗口

时间:2013-12-13 09:28:11

标签: windows batch-file selenium cmd

我正在整理一个批处理文件,该文件执行以下操作:

  1. 启动Selenium测试中心
  2. 启动Selenium测试节点
  3. 运行Selenium测试脚本
  4. 打开测试结果xml doc。
  5. 关闭Selenium hub和节点
  6. 通过使用START命令调用2个批处理文件来打开Selenium hub和节点,以便它们在自己的cmd窗口中打开。我对我的过程一直很好,直到第5点,关闭包含集线器和节点的两个cmd窗口。

    我知道你可以关闭所有“cmd.exe”进程,但这似乎是一种直接的工具。有没有办法识别或保持我的批处理脚本打开的集线器和节点cmd窗口的句柄,以便我可以只关闭那两个?

    感谢。

2 个答案:

答案 0 :(得分:1)

尝试这样的事情:

@echo off

for /f "tokens=2 delims=;= " %%a in ('wmic process call create "cmd.exe /c C:\selenium.bat"^,"c:\workdir" ^| find "ProcessId"') do (
    set "cmd_pid=%%a"
)

taskkill /PID %cmd_pid% /f

使用wmic process call create "cmd.exe /c C:\selenium.bat"您可以启动一个进程并获取其PID,当您杀死它时,您可以使用taskkill /PID %cmd_pid% /f

答案 1 :(得分:1)

作为精彩的npocmaka答案的替代方案,如果您知道或可以设置已启动的Selenium窗口的标题,可以使用此信息来检索和过滤带有这些标题的任务列表。

当输出到控制台正确时,从taskkill line

中删除echo命令
@echo off

    rem Prepare environment
    setlocal enableextensions

    rem Configure list of window titles
    set titles="seleniumHUB" "seleniumNODE"

    rem For each title in title list
    for %%t in (%titles%) do (

        rem Get the list of task with the indicated window title. 
        rem The list is get from tasklist, in verbose mode and in csv format 
        rem The last column in the list is the window title 
        for /f "tokens=1,2 delims=," %%a in (
            'tasklist /fi "imagename eq cmd.exe" /v /fo csv ^| findstr /i /r /c:"%%~t[^,]*$"'
        ) do (
            echo taskkill /pid %%~b
        )
    )

    rem Clean
    endlocal
相关问题