-1

I want to open chrome using bat file, but when chrome specific process closes the cmd should close as well. I've tried it but doesn't seem to work.

"C:\Users\andreas\AppData\Local\Chrome\Application\chrome.exe"

Shouldn't the bat file be attached to the app and when the app closes the cmd window should close as well?

shak
  • 111
  • 1
  • 3
  • Possible duplicate of [How can I run a program from a batch file without leaving the console open after the program start?](https://stackoverflow.com/questions/324539/how-can-i-run-a-program-from-a-batch-file-without-leaving-the-console-open-after) – phuclv Feb 27 '18 at 13:54

2 Answers2

0

No it won't. Starting programs using or not using start is documented in start /? help.

When executing an application that is a 32-bit GUI application, CMD.EXE
    does not wait for the application to terminate before returning to
    the command prompt.  This new behavior does NOT occur if executing
    within a command script.

Also see http://stackoverflow.com/questions/41030190/command-to-run-a-bat-file/41049135#41049135

New behaviour refers to the Win NT4 vs Win 2000 versions of CMD.exe.

ACatInLove
  • 175
  • 3
  • Thanks for the response. So there is no way to close the cmd window when i close for example the chrome browser which was initiated from the cmd? – shak Feb 27 '18 at 09:58
  • You can use `start` to override default behaviour. See `start /?`. – ACatInLove Feb 27 '18 at 23:01
0

You could use start /WAIT, designed to wait for GUI application to close, but it won't solve your problem. (It would for Firefox, for example).

The thing is, when you start Chrome, it actually acts as a launcher and spawns a bunch of children processes, and then exits immediately, leaving them to show you a page.

So, if you ran start /WAIT ...\chrome.exe, it would exit at that point.

What can be done about that?
You can monitor chrome instances checking the process list like that:

launch_chrome.bat

@echo off
start /wait "chrome" "C:\Users\andreas\AppData\Local\Chrome\Application\chrome.exe"

:loop
tasklist /FI "ImageName eq chrome.exe"  | find /i "chrome" > nul || goto :chrome_exited 
:: wait 2 seconds before checking again
timeout /t 2 >nul
goto :loop

:chrome_exited
echo Chrome exited

Still, it won't solve your problem if you want it to run along with your already running usual chrome processes. The script will wait for you to close all of them.

In this case the options left for you are to use tools like AutoIt, AutoHotkey.
They can check the titles on all the chrome windows and if they don't see some specific string they can close the batch.

wolfrevokcats
  • 2,100
  • 1
  • 12
  • 12