Batch file - Closing separate cmd windows opened by a batch file process Batch file - Closing separate cmd windows opened by a batch file process selenium selenium

Batch file - Closing separate cmd windows opened by a batch file process


As an alternative to the wonderful npocmaka answer, if you know or can set the title of the started Selenium windows, can use this information to retrieve and filter the list of tasks with those titles.

When output to console is correct, remove the echo command from taskkill line

@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


Try something like this:

@echo offfor /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

with wmic process call create "cmd.exe /c C:\selenium.bat" you can start a process and get its PID and when you to kill it you can use taskkill /PID %cmd_pid% /f