Tfs post-build step running application hangs - batch-file

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.

Related

Writing Batch file that monitors a certain web page's status

The issue
I am trying to create a batch file that will execute the following two commands:
cd /some/path
dotnet run
that produces the following output:
It starts my localhost server.
Trying To Accomplish
What I would like to do is, put those two commands in a batch file and automatically open Chrome to the server address, HOWEVER, the dotnet command takes some time to finish. So somehow, I would have to keep monitoring localhost to see if it is available.
Any help would be appreciated. Although opening a CMD window, typing those 2 commands, and waiting a minute isn't all that much of a problem, it sure would be nice to just click on a batch file.
Thank you.
You can create a batch file like this code :
#echo off
Set "ApplicationPath=%UserProfile%\source\repos\PruttPos\PruttPosSystem\"
CD /D "%ApplicationPath%"
dotnet run
Start "Chrome" Chrome.exe "http://localhost:5000"
pause

Call chrome from bat and close bat when chrome browser closes

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.

Start Command not allowing additional process to be created

My Program opens a javascript server for listening. However when i run this in cmd
start "Runnning" /B CMD /C CALL "file\program.exe" 23 12
The Program is prevented from creating the process for the Javascript program. Any work arounds that work in (.bat) files are appreciated

TFS Auto Build hangs waiting for termination of BAT file

I have a TFS Build Definition.
In the work flow, I need to bring up a console listener, and run some tests on this listener.
So I create a BAT file with the followings:
start cmd /k "d:\abc.exe"
If I run this BAT file, the BAT itself will terminate, but it will spawn another cmd Windows, running the listener. So all is fine.
But when this is incorporated into TFS Build Definition, the work-flow would wait for the completion of this process, and the entire flow would hang.
I've tried with various switches for both START and CMD so that the work-flow can continue with the listener running, but to no avail.
start "" cmd /c "d:\abc.exe"
in cmd, /k means start a new instance, execute the indicated command and leave the window open. /c means is the same, but when command ends, cmd exits.

How to start two servers (mysqld and thin) in one batch file?

I want to create a batch file to start MySQL server (mysqld) and Rails Thin server (rails s thin). But only MySQL starts, rails s thin doesn't:
#ECHO OFF
cd C:\path\to\app
CMD /C "mysqld --console"
REM exit <-- with or without it
REM cd C:\path\to\app <-- with or without it
CMD /C "rails s thin"
I also tried to create three files for this. The main one runs other two. Doesn't help either, only MySQL runs. And I need to start MySQL before Rails. Rails needs running MySQL. How to write such a batch?
UPD:
How to start 2 programs simultaniously in windows command prompt
use start
e.g.
start notepad.exe
start calc.exe
if you want to run one after another with some tests, you may need to write your own program.

Resources