It's all about the answers!

Ask a question

How to get the port number from a scm daemon start command sent in a batch file


Richard Good (872861) | asked Nov 08 '16, 7:19 a.m.

I am trying to start a daemon, do some work with it then terminate it on completion

I can do

start "my name" scm daemon start --port 49000

and then

scm daemon stop --port 49000

but cannot redirect the console application like so : -

start "my name" scm daemon start 2>%TEMP%\portLoc.txt

or

start "my name" scm daemon start >%TEMP%\portLoc.txt

or about three other variations used to get the port number, I don't want to kill all instances of scm. The number I want is there in the console window, but I can't get at it. I'd like not to hard code the port number to avoid using a port already in use or maybe errors when running the application twice. Any hints? Thanks

Richard

2 answers



permanent link
Alan Sampson (93749) | answered Nov 08 '16, 1:07 p.m.
JAZZ DEVELOPER
It looks like you need to escape the redirection character.

See http://superuser.com/questions/338277/windows-cmd-batch-start-and-output-redirection for some hints and tips.

Comments
Richard Good commented Nov 08 '16, 1:15 p.m.

Did see that, when I add the escape characters, the process fires up for a second then closes down, so no dice. Thanks for the attempt


permanent link
Donald Nong (14.5k614) | answered Nov 08 '16, 8:23 p.m.
Try this:
start /b scm.exe daemon start > myName.log 2>&1

I understand that you will then find out the port number in the file myName.log for the use of "scm daemon stop". If the command works for you, you will not get any new command windows, so the title of the start command "my name" is of no use, and you should use the log file name to identify your daemon.


Comments
Richard Good commented Nov 09 '16, 7:41 a.m. | edited Nov 09 '16, 6:51 p.m.

Not enough chars to add this as a comment

Thanks, that helps, but I am loathe to mark it the right answer as I still can't get the port as the file is locked by the scm process somehow and stalls the system, delayed expansion **** in batch files can also bite you, powershell is the way ahead, but not for this customer!! Try running something like the following and tell me what I am doing wrong!

echo on
setlocal ENABLEDELAYEDEXPANSION
::set lscmLoc=B:\RTC-Client-Win-5.0.2-32bit\jazz\scmtools\eclipse\lscm.bat
::set scmLoc=B:\RTC-Client-Win-5.0.2-32bit\jazz\scmtools\eclipse\scm

set lscmLoc=C:\Utilities\RTC-With-DesManager\jazz\scmtools\eclipse\lscm.bat
set scmLoc=C:\Utilities\RTC-With-DesManager\jazz\scmtools\eclipse\scm
::you should run this from an RTC sandbox, scm assumes this, I am assuming you have a sandbox in c:\temp here...
c:
cd temp

::works but hard codes the port, port could be being used for something else, probably better than kiling everything though
::start "close me on completion" %scmLoc% daemon start --port 49403

start /b %scmLoc% daemon start > %TEMP%\portLoc.txt 2>&1
::set portLoc=<%TEMP%\portLoc.txt

cd %TEMP%
FOR /F "tokens=2" %A in ('portLoc.txt') do set ric=%A

set ric
echo %ric%

::wait for process to start up
::ping 127.0.0.1 -n 10 >nul
:: echo Login...
::call %lscmLoc% login -r https://server:9443/ccm/ -n serverRep -u b51648 -P b51648

:: List snapshots
::call %lscmLoc% list snapshots -r serverRep -m 1000 "JPT_Dev_Stream"

:: List components
::call %lscmLoc% list components -r serverRep -m 1000 -s "SS_MPU_0001"

:: echo Logout...
::call %lscmLoc% logout -r serverRep

::kils just one daemon
%scmLoc% daemon stop --port %ric%
endlocal


Richard Good commented Nov 09 '16, 7:45 a.m. | edited Nov 09 '16, 7:47 a.m.

I have added an answer, but it was intended as a comment. I have tried using exclamation marks around  variables, found that notepad is somehow instantiated when I load the file containing the port number using a for loop, nothing seems to allow me to get and process the port numbers, batch files are a little before my time so could be doing something stupid, not sure what, don't usually get stuck on something apparently simple for a couple of hours!! delimiters are said to default to spaces and tabs so I have left that out the parameter I want should be token 2. Also tried running this direct and as a batch file with the requisite number of % signs


Donald Nong commented Nov 10 '16, 2:51 a.m.

Nothing really "wrong" with it, as you've already narrowed down what's not working. We just need to work around it. Apparently the FOR command opens the file in "rw" mode and it fails. So we just need to open the file in "r" mode, same as you can open the file in notepad. I change this line

FOR /F "tokens=2" %A in ('portLoc.txt') do set ric=%A
to
set /P port= < portLoc.txt
FOR /F "tokens=2" %%A in ("%port%") do set ric=%%A
and it works for me.

Note 1: during testing, I need to add some delay (using ping) before the set command to allow the file to be created, otherwise I get blank content.
Note 2: The correct syntax for a variable for the FOR command in a batch file is %%variable, instead of %variable. Check the help content for details.

Your answer


Register or to post your answer.


Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.