Search code examples
google-chromebatch-filetaskkill

kill only once instance of chrome from command line


I have a batch file that runs to open a webpage in Chrome that runs a specific function.

start http://www.example.com/cgi/myprogram.exe

This process runs quickly, then I want to automatically close the browser window. I don't want to use taskkill /IM chrome.exe because Chrome has many services running under "chrome.exe" and I only want to kill the one that shows on the applications tag of task manager, not the processes tag.

Is that possible?


Solution

  • To just kill the new tab, you can use this:

    @echo off
    setlocal EnableDelayedExpansion
    set "newPIDlist="
    set "oldPIDlist=p"
    ::find old PIDs
    for /f "TOKENS=1" %%a in ('wmic PROCESS where "Name='chrome.exe'" get ProcessID ^| findstr [0-9]') do (set "oldPIDlist=!oldPIDlist!%%ap")
    ::start your site here
    start http://www.example.com/cgi/myprogram.exe
    ::find new PIDs
    for /f "TOKENS=1" %%a in ('wmic PROCESS where "Name='chrome.exe'" get ProcessID ^| findstr [0-9]') do (
    if "!oldPIDlist:p%%ap=zz!"=="%oldPIDlist%" (set "newPIDlist=/PID %%a !newPIDlist!")
    )
    echo %newPIDlist%
    ::wait for page to load
    timeout /t 5 /nobreak >nul
    taskkill /f %newPIDlist% /T > NUL 2>&1
    

    However, note that this won't close the tab, it will just kill the process of the tab, causing an error message to pop up. As discussed here, closing a single google chrome tab from the command line is not possible.