Jenkins hangs running cmd that calls tera term - windows

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

Related

How to get return/exit code of a batch file started with START command?

I am trying to run a batch file with start /high and still get the return/exit code, i.e. %ERRORLEVEL%. The problem seems to be that command START does not return the exit code that the batch file returns.
We have a simple batch file for testing named BatFileThatReturnsOne.bat.
The contents of BatFileThatReturnsOne.bat are
EXIT /B 1
We are calling this as such:
start /high /wait BatFileThatReturnsOne.bat
But no matter what the batch file returns, the execution of start never has a %ERRORLEVEL% of anything other than 0 (zero).
This is all actually called by cfn-init in CloudFormation, but that is probably not relevant because we can reproduce it from a command line window.
The actual call is:
cmd.exe /C start /high /wait BatFileThatReturnsOne.bat
How do I get start to set the %ERRORLEVEL% to something other than 0 (zero)?
directly from a cmd window or a batch file you can use
start /high /wait cmd /c BatFileThatReturnsOne.bat
but if you need to start the cmd instance to execute the start command that launchs the batch file then you can use
cmd /v /e /c" start /high /wait cmd /c launched.cmd & exit ^!errorlevel^!"
Just change EXIT /B 1 by EXIT 1.
As explained in the Table 4 given in this answer about START /WAIT bat command:
When the started Batch file ends, set ERRORLEVEL = value from 'EXIT number' commmand.

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%)"

Start cmd, run exe and after the program ends run command pause and then exit

How can I open a new command promt and run in there an exe and after the program (exe) ends run command pause and then exit
Exe is running by:
start cmd /k bla.exe
After bla.exe is done, I want to run a pause command. I tried:
start cmd /k bla.exe & pause
but pause is running in the initial console.
How can I fix this?
The solution is:
start "titel" cmd /k "bla.exe & pause & exit"

"start %comspec% /c script.cmd" vs "start cmd /C script.cmd"

What is the difference between following commands:
start %comspec% /c script.cmd
start cmd /C script.cmd
I need that cmd window for script.cmd should close automatically when script.cmd is finished.
%comspec% just points to cmd.exe, so both commands will do the same thing. Other than that /C is correct, this will close the command prompt after execution
The difference is that "%comspec%" should expand to the default command interpreter, whereas "cmd" searches for a cmd executable and invokes it. Most of the time they are the same.
But in case there is a cmd executable (cmd.exe, cmd.bat, etc) in the current directory, than that cmd executable will be invoked.
I would go with using %comspec%
For a not admin user on WindowsXP-SP2.
%comspec% starts in %WINDIR%\System32.
'cmd' starts in user's home e.g. C:\Document And Settings\USER1.

How to set working directory of cmd, at the same time as executing a program

I currently have a command that I thought did this:
START /WAIT cmd.exe /d tester test.cmd
Although it does not appear to work, it only changes the directory, not executing test.cmd
Try with:
start /wait /d tester cmd.exe "/c test.cmd"
Or simply:
start /wait /d tester test.cmd

Resources