简体   繁体   English

如何从 .bat 文件运行多个服务

[英]How to run multiple services from .bat file

I would like to run selenium server from bat file.我想从 bat 文件运行 selenium 服务器。 It means start hub and two nodes under the hub.表示启动集线器和集线器下的两个节点。 On Ubuntu I have this script to do that:在 Ubuntu 我有这个脚本来做到这一点:

  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.但是在 Windows 上,由于某些原因,这不起作用。 I found something like this for Win but still no success.我为 Win 找到了类似的东西,但仍然没有成功。 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/" 

在此处输入图像描述

Thanks for any help.谢谢你的帮助。

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.但是以下仍然会失败,因为引号不能嵌套,所以&没有被引用,并且初始批处理解析器将该行视为两个连接的命令而不是单个start命令。

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.请注意,默认情况下cd不会更改您的活动驱动器。 So say your active drive is D: , then the above will still not work.所以说你的活动驱动器是D: ,那么上面的仍然行不通。 You would have to use either cd /d "c:\Program Files\Selenium\Server" , or else pushd "c:\Program Files\Selenium\Server" .您必须使用cd /d "c:\Program Files\Selenium\Server"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.但是在您的start命令之前cd /dpushd可能更简单,因此您只需执行一次。 START ed processes inherit the environment of the parent. START ed 进程继承父进程的环境。

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 .服务终止后,子进程不需要保持打开状态,因此最好使用cmd /c而不是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 .但我不确定您是否真的需要在java内显式运行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.最后,您可能不需要为每个进程使用单独的 window,在这种情况下,您可以将/B选项添加到START命令。 Put that immediately after start . start后立即放置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM