windowsbatch-fileterminalterminal-services

How to run multiple services from .bat file


I would like to run selenium server from bat file. It means start hub and two nodes under the hub. On Ubuntu I have this script to do that:

  java -jar $jarFilePath -role hub & 
  java -jar $jarFilePath -role node -hub http://173.249.58.30:4444/grid/register/ & 
  java -jar $jarFilePath -role node -hub http://173.249.58.30:4444/grid/register/ & 
  exit 0

It is necessary to chain it with & cause the first command is still running and does not run next commands. But on Windows this does not work for some reasons. I found something like this for Win but still no success. It opens three terminal but nodes are not able to register to hub:

start cmd.exe /k "cd c:\Program Files\Selenium\Server & java -jar selenium-server-standalone-3.141.59.jar -role hub"

start cmd.exe /k "cd c:\Program Files\Selenium\Server & java -jar selenium-server-standalone-3.141.59.jar -role node -hub http://192.168.137.1:4444/grid/register/" 
start cmd.exe /k "cd c:\Program Files\Selenium\Server & java -jar selenium-server-standalone-3.141.59.jar -role node -hub http://192.168.137.1:4444/grid/register/" 

enter image description here

Thanks for any help.


Solution

  • The reason your commands are failing is because paths with spaces must be quoted (space is a token delimiter).

    But the following will still fail because quotes cannot be nested, so the & is not quoted and the initial batch parser treats the line as two concatenated commands instead of a single start command.

    For example, looking at the first line, this will not work

    start cmd.exe /k "cd "c:\Program Files\Selenium\Server" & java -jar selenium-server-standalone-3.141.59.jar -role hub"
    

    To fix the above, many people would escape the &

    start cmd.exe /k "cd "c:\Program Files\Selenium\Server" ^& java -jar selenium-server-standalone-3.141.59.jar -role hub"
    

    But I prefer to escape the outermost quotes so that I can write the commands as I would type them myself into the command prompt

    start cmd.exe /k "cd ^"c:\Program Files\Selenium\Server" & java -jar selenium-server-standalone-3.141.59.jar -role hub^"
    

    Note that cd does not change your active drive by default. So say your active drive is D:, then the above will still not work. You would have to use either cd /d "c:\Program Files\Selenium\Server", or else pushd "c:\Program Files\Selenium\Server".

    But it is probably simpler to cd /d or pushd before your start commands so that you only have to do it once. STARTed processes inherit the environment of the parent.

    There is no need for the sub-process to remain open after the service terminates, so better to use cmd /c instead of cmd /k.

    Your full script could be

    @echo off
    pushd "c:\Program Files\Selenium\Server"
    start cmd.exe /c "java -jar selenium-server-standalone-3.141.59.jar -role hub"
    start cmd.exe /c "java -jar selenium-server-standalone-3.141.59.jar -role node -hub http://192.168.137.1:4444/grid/register/" 
    start cmd.exe /c "java -jar selenium-server-standalone-3.141.59.jar -role node -hub http://192.168.137.1:4444/grid/register/" 
    popd
    

    But I'm not sure you actually need to explicitly run java within cmd.exe. You may be able to simply use the following

    @echo off
    pushd "c:\Program Files\Selenium\Server"
    start java -jar selenium-server-standalone-3.141.59.jar -role hub
    start java -jar selenium-server-standalone-3.141.59.jar -role node -hub http://192.168.137.1:4444/grid/register/
    start java -jar selenium-server-standalone-3.141.59.jar -role node -hub http://192.168.137.1:4444/grid/register/
    popd
    

    Finally, you likely don't need a separate window for each process, in which case you can add the /B option to the START command. Put that immediately after start.