Call chrome from bat and close bat when chrome browser closes - batch-file

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?

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.

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.

Related

Opening separate web windows using a batch file

I would like a batch file to open separate internet windows in order to keep relevant information grouped without having a ton of tabs.
I can open a single internet window doing this:
#Echo Off
start chrome.exe
exit
I can open two separate windows doing this:
#Echo Off
start chrome.exe
start chrome.exe
exit
However, as soon as I add a web link to either of these, the window will not open. Instead, a new tab is created in whatever window was open first it would seem:
#Echo Off
start chrome.exe "www.Yahoo.com"
start chrome.exe "www.Google.com"
Exit
These commands will open separate windows if there are no URLs, but they will open a new tab for the link if you include the URLs, usually in the first window that is/gets opened...
I just discovered -new-window here.
I will try that and report back.
#Echo Off
start chrome.exe -new-window "www.Yahoo.com"
start chrome.exe -new-window "www.Google.com"
Exit
This is what I needed. It opens separate windows for each without adding new tabs to a different window.

ConEmuC: Root process was alive less than 10 sec, ExitCode=0

I was trying to create a shortcut on my Windows desktop to ConEmu64.exe that launches with a script I wrote called "MyStartUpScript.cmd".
In Target field of the Windows shortcut, I have:
"C:\Program Files\ConEmu\ConEmu64.exe" /cmd path/to/MyStartUpScript.cmd
However, this does not work. Specifically, it launches a new instance of ConEmu, runs my script, but then it says "ConEmuC: Root process was alive less than 10 sec, ExitCode=0"
What is going on?
First. Learn about cmd.exe switches. When you run your "startup script" that way you tell cmd.exe to execute your script and immediately exit. I doubt it was your intention.
cmd /? would give you an answer.
"C:\Program Files\ConEmu\ConEmu64.exe" /cmd cmd.exe /k path/to/MyStartUpScript.cmd
It seems that open conEmu with admin rights solve the problem (right click -> "run as adminsitrator").

Batch file start command launching process but not launching GUI window

So i'm trying to do a simple task restart on a box that needs a GUI app running on the desktop. I'm using the start command to call the application. For some reason when i run it from the task scheduler it does everything it's supposed to except launch the application's GUI window. I can see the process running in Task Manager, but no GUI launches. This is Windows 2008 R2.
Here's the script replacing the app with notepad (which has the same problem).
#echo off
:: Kill notepad.exe if running.
TaskKill /IM notepad.exe /F
:: Wait for app to close.
PING 1.1.1.1 -n 1 -w 8000 >NUL
:: Check to make sure app isn't running, then start it.
tasklist /FI "IMAGENAME eq notepad.exe" 2>NUL | find /I "notepad.exe">NUL
IF NOT "%ERRORLEVEL%"=="0" START "" "notepad.exe"
EXIT %ERRORLEVEL%
Is this a Windows 2008R2 Task Scheduler gotcha, or a batch file gotcha?
You must have the option to "Run only when user is logged in" selected. Otherwise the task will be launched as an invisible background process.

Tfs post-build step running application hangs

We have a configured TFS CI which is making our build. After a build I`d like to have my simple executable application to be deployed to specific folder on the server and then started.
I decided to do this with post-build step and batch script. Everything works perfectly except one:
when the application is stared, build agent(the one who runs my script) hangs.
Here is how I start my application from the script:
start /b %depldir%\MyApp.exe [params] > log.txt
So I start it in backgroound and redirect std out/error to file.
Application is started correctly but build process won`t finish untill I kill the application.
How do I start my application without build agent waiting for it to finish?
I'm not sure if this is a bug but if you use start /b something > logfile.txt the new and hidden cmd window is started with parameter /k. This forces the hidden window to stay active. This makes the calling bat wait for it to exit and this is why your build won't terminate. To verify this I've created two files:
starter.bat:
start /b tostart.bat > log.txt
tostart.bat:
echo started
When I start starter.bat the cmd doesn't terminate and in the task manager the following process appears:
cmd.exe USER 00 00:00:00 1.400k C:\Windows\system32\cmd.exe /K
tostart.bat
/K means:
/K Run Command and then return to the CMD prompt.
This is useful for testing, to examine variables (taken from http://ss64.com/nt/cmd.html)
To cut a long story short: it works fine when you replace start /b with call. Unfortunately call doesn't have a parameter like /b so your application won't start in background. I don't know whether this is a problem for you.
If it is: I've faced similar problems with my Jenkins build server. My solution was to create a task which runs my application (in background). Instead of calling the program itself I just call SCHTASKS /Run /TN "TASK_NAME". This command just triggers the task without waiting. This way you can also avoid problems with permissions etc.

batch-file - batch file to check for open browser script

Right now I have a batch file I created that simple kills a chrome browser and opens up a new one to a particular homepage. I run this on a 5 minute interval with some added software that runs it if there is no activity for 5 minutes. I use this as a timeclock.
I am having some issues with the browser closing and reopening all the time. I figure I can make this script more effiecent by changing a few things.
Instead of closing and open a new browser every 5 minutes I would like to check first if the chome browser is already open and if it is then simple refresh it, otherwise open it.
Here is my current script
#echo off
taskkill /f /im chrome.exe
start "chrome" "C:\Program Files\Google\Chrome\Application\chrome.exe" --kiosk "http://www.example.com/sd/clockin/testclockin.php"
To check if Chrome is currently running, you can use the tasklist command.
tasklist | findstr chrome.exe || echo Chrome is not running
the code after || will only run if an errorlevel is set by the previous command, i.e. if chrome.exe doesn't show up in tasklist. You can use && to execute only if an errorlevel was not set.
You can also just check the errorlevel using a simple if statement -
if %errorlevel%==1 echo Chrome is running
Obviously chrome is closing because you are running the taskkill command. I assume you want to refresh the open page. You may need to look into using VBScript for this. This should help - refresh firefox from .bat file/ command.

Resources