Change the title in the cmd prompt using batch file - shell

I created a .bat file which executes multiple calls in different command prompt.
#echo off
title Start multiple services
start cmd /k Call mongod
start cmd /k Call elasticsearch
timeout /t 5
start cmd /k Call kibana
cd "C:\New folder\influxdb-1.7.6_windows_amd64\influxdb-1.7.8-1\"
start influxd.exe
timeout /t 10
cd "C:\App\App1\bin"
start cmd /k node www
cd "C:\App\App2\server"
start cmd /k node server.js
cd "C:\App\App3\server"
start cmd /k node server.js
cd "C:\App\App4\server"
start cmd /k node server.js
the issue what I am facing that for App2, App3 and App4. I am getting the same title "node server.js" in the command prompt which is creating confusion. How could I add different names in the title for these. I tried putting TITLE in the command like start cmd /k TITLE App3 node server.js. But that didn't work.

You can do the same by
START /D C:\App\App2\bin "write your title" cmd /k node server.js
For reference check: https://ss64.com/nt/start.html

Related

Strange behavior when running start.bat file in Windows 2019 Core

I have a start.bat which does nothing but
#echo off
Echo "Hello World"
Then I open a cmd window and type in
start "" /D D:\Test start.bat
it opens a new cmd window with "Hello World", but does not close the window automatically.
Now I created another bat file named start2.bat which has the same content as the start.bat above. Then I type in
start "" /D D:\Test start2.bat
it opens a new cmd window with "Hello World", and does close the window automatically.
Why is it happening ???
I try to use some trace tool to analyse, from the tool, it seems like my system turn start "" /D D:\Test start.bat to cmd.exe /K start.bat
TraceTool
Tried in another computer, now, no matter which file name I used, it turns into cmd/exe /K start.bat
start3.bat
Update:
Turns out adding /C does not work for me, attach process tree below, the system still changes /C to /K
Process tree view with /C
Update:
Provide full process view. My start.bat is exactly the same as above
And now I open a cmd.exe window, and typed in
"c:\windows\system32\cmd.exe" /C START "test" /D "C:\test" start.bat or just cmd /C START "test" /D "C:\test" start.bat
And
"c:\windows\system32\cmd.exe" /C START "test" /D "C:\test" start2.bat or just cmd /C START "test" /D "C:\test" start2.bat
From the process tree view, it can be seen that the first command turns into
C:\windows\system32\cmd.exe /K start.bat
While the second command turns into
C:\windows\system32\cmd.exe /c "c:\test\start2.bat" "
cmd.exe
Full Process Tree View
From the usage information of the start command (excerpt from the description of the command/program argument):
[...]
command/program
If it is an internal cmd command or a batch file then
the command processor is run with the /K switch to cmd.exe.
This means that the window will remain after the command
has been run.
If it is not an internal cmd command or batch file then
it is a program and will run as either a windowed application
or a console application.
[...]
Hence when starting an internal command or a batch file, cmd.exe together with its /K option is used, so start "" "script.bat" is equivalent to:
start "" cmd.exe /K "script.bat"
If you want the new console window to become closed, use this instead:
start "" cmd.exe /C "script.bat"

Jenkins hangs running cmd that calls tera term

I have a test.cmd file(does some tests and calls teraterm) that runs correctly on both command console and powershell console
When I try to call this .cmd file with Jenkins, script runs but Jenkins hangs.
There is specific foo_hangs.cmd script that always hangs the test, if this script is removed Jenkins does not freeze.
I tried calling the foo_hangs.cmd with "Call", script failed to execute
I tried calling the foo_hangs.cmd as below , script ran but Jenkins hangs..
start /wait CMD /c "foo_hangs.cmd"
start /wait CMD /k "foo_hangs.cmd"
start CMD /c "foo_hangs.cmd"
start CMD /k "foo_hangs.cmd"
powershell -Command "Start-Process 'foo_hangs.cmd'"
test script is called from Jenkins execute Windows Batch command using below command
cmd /c D:\foo\test.cmd
test.cmd script is looks like this
d:
cd D:\foo
start /wait "test1" CMD /c "foo_nothangs.cmd"
start /wait timeout 10
start /wait "test2" CMD /c "foo_hangs.cmd" -- after this line test starts and ends successfully but hangs Jenkins
start /wait timeout 10

Use a batch file to automatically start 5 separate cmd prompts, where each cmd prompt points to a different directory and starting the php web server

I hope you can help me out with this issue, which I thought would be quite trivial but is turning out to be quite a bugger.
As mentioned in the subject line this is what I want to accomplish:
Automatically run 5 command prompts, each new command prompt needs to do the following
Change directories to a specific location
start the built in php web server
This is the code I currently have for my .bat file:
start cmd /k
cd "directory 1"
php -S localhost:8081
start cmd /k
cd "directory 02"
php -S localhost:8082
start cmd /k
cd "directory 03"
php -S localhost:8083
etc...
Update (here are the working commands after some tweaking)
start "Dir1" /d "directory1" cmd /k "php -S localhost:8081"
start "Dir2" /d "directory2" cmd /k "php -S localhost:8082"
start "Dir3" /d "directory3" cmd /k "php -S localhost:8083"
start "Dir4" /d "directory4" cmd /k "php -S localhost:8084"
start "Dir5" /d "directory5" cmd /k "php -S localhost:8085"
start "Dir6" /d "directory6" cmd /k "php -S localhost:8086"
Thanks very much.
Cheers,
Tim K
The command has to be on the same line as cmd /k, else you start a new window and execute the next line in the original window. Start has a /d switch to set the working folder for the new process, so you don't need cd:
start "Dir1" /d "directory 1" cmd /k "php -S localhost:8081"
REM etc.
When the folder names and ports are accurate, you can use a for /L loop to simplify things:
for /l %%i in (1,1,5) do start "Dir %%i" /d "directory %%i" cmd /k "php -S localhost:808%%i"

batch windows command written in prompt but no execution

To avoid to repeat a task too often, I am setting up a batch file (in WINDOWS 10). It opens several CMD PROMPT to a specific Directory and launch a command.
For one case, I want the CMD PROMPT to open, to go to the specific directory and to set the COMMAND in the PROMPT without launching it. Then I'd just have to click on ENTER to launch that command whenever I want later on.
Here is my code:
setlocal ENABLEEXTENSIONS
set CordovaProjPath="C:\MyPath\"
start cmd /k "cd /d %CordovaProjPath% && cordova build android"
With this code it launches the command "cordova build android".
If I go with start cmd /k "cd /d %JCACordovaProjPath% instead of start cmd /k "cd /d %JCACordovaProjPath% && cordova build android" it gives me the PROMPT with: "C:\MyPath>", I'd like to write: "cordova build android" behind it without launching the command.
Any idea?
To provide repeatable execution (as mentioned in comments) you can put the relevant commands in a loop with a "quit" option:
#Echo Off
setlocal
Set "CordovaProjPath=C:\MyPath"
Set "CommandToRun=cordova build android"
:loop
Cd /D %CordovaProjPath%
Echo %CommandToRun%
set QUIT=
set /p QUIT=Press ENTER to run command or 'Q' to quit:
if /i "%QUIT%" == "Q" goto :eof
%CommandToRun%
goto :loop
Unlike the original, this runs the target command in the same command-window as the repeating loop. Depending on what the command in question does, this may be more attractive (less windows popping-up). However, some commands may cause the main window to close; if this is the case, you can revert to running the command in its own window in one of two different ways. In each case, replace the line:
...
%CommandToRun%
...
Run in own window and remain open
...
start "%CommandToRun%" /wait cmd /k %CommandToRun%
...
Using /k will leave the command-prompt window open after the target command has run -- this may be appropriate if you need to see the output of the command and it does not have its own pause.
Run in own window then close
...
start "%CommandToRun%" /wait cmd /c %CommandToRun%
...
Using /c will mean the command-prompt will close after the target command has run. This may be appropriate if you do not need to see the output of the command, or if it has its own pause.
Would something like this do you:
#Echo Off
Set "CordovaProjPath=C:\MyPath"
Set "CommandToRun=cordova build android"
Start "%CommandToRun%" Cmd /K "Cd /D %CordovaProjPath%&Echo %CommandToRun%&Pause>Nul&%CommandToRun%"
Below is an alternative which may allow for your alternative double-quoting method:
#Echo Off
Set CordovaProjPath="C:\MyPath"
Set CommandToRun="cordova build android"
Start %CommandToRun% Cmd /K "(Cd /D %CordovaProjPath%)&(Echo %CommandToRun%)&(Pause>Nul)&(%CommandToRun%)"

How can use Windows cmd/bat file to open various apps at once and close the terminal windows?

I am using this:
start /b cmd /c "C:\Windows\notepad.exe"
start /b cmd /c "C:\Program Files\Google\Chrome\Application\chrome.exe"
start /b cmd /c "C:\Program Files\Skype\Phone\Skype.exe"
exit
The above works but keeps the prompt/terminal window open. How can I close it from inside the bat/cmd file as soon as the last app is open?
Try the following instead:
C:
cd \Windows
start notepad.exe
cd "\Program Files\Google\Chrome\Application"
start chrome.exe
cd "\Program Files\Skype\Phone"
start Skype.exe
exit
it is probably the fact that your starting cmd - just start the application

Resources