Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 869
Shawn Avatar asked Dec 14 '25 03:12

Shawn


1 Answers

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.

like image 125
Dennis van Gils Avatar answered Dec 16 '25 22:12

Dennis van Gils



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!