[英]How to start spring boot jar files one by one using windows batch file?
I would like to start spring boot jar files one by one using windows batch file. 我想使用Windows批处理文件一个一个地启动spring boot jar文件。 I have created a batch file like below:
我创建了一个如下所示的批处理文件:
start java -jar service1.jar --spring.profiles.active=local
start java -jar service2.jar --spring.profiles.active=local
When I ran this batch file, both jar files are running at once. 当我运行该批处理文件时,两个jar文件都同时运行。 But I have to run service1.jar file first and once its up and running then I need to run service2.jar file.
但是我必须先运行service1.jar文件,一旦它启动并运行,那么我需要运行service2.jar文件。
Can anyone please help me with this or any other way ? 任何人都可以通过这种方式或其他方式帮助我吗?
Using start in a batch file doesn't wait for the process - it is kind of like starting a new thread. 在批处理文件中使用start不会等待进程-这有点像启动新线程。
There is another function call (or start /wait ) but these will work if it wasn't services. 还有另一个函数调用(或start / wait),但是如果没有服务,这些函数将起作用。 The problem you have is that the services need to keep running (you can't wait for them to finish with start /wait).
您遇到的问题是服务需要继续运行(您不能等待它们以启动/ wait结束)。
So you need to implement some kind of condition and a check in your batch file which when true to start service 2. For example if that's a web service and has a get method that can be used to check if it is working - call that method and wait for the result before starting the second service. 因此,您需要实现某种条件并在批处理文件中执行一项检查,该检查为true时才能启动服务2。例如,如果这是一个Web服务,并且具有可用于检查其是否正常工作的get方法-调用该方法并等待结果,然后再启动第二项服务。
You can do something like: 您可以执行以下操作:
start java -jar service1.jar --spring.profiles.active=local
:isStarted
set ready=false // Here you implement some logic to check if the service is running
if %ready%=="false" goto isStarted
start java -jar service2.jar --spring.profiles.active=local
Another solution would be to make a separate java program that tests service1 and exits when it is up and running. 另一个解决方案是制作一个单独的Java程序来测试service1并在其启动并运行时退出。 If we call it "service1test" then your bat file will be:
如果我们将其称为“ service1test”,则您的bat文件将为:
start java -jar service1.jar --spring.profiles.active=local
service1test - without using start so it can wait
start java -jar service2.jar --spring.profiles.active=local
A very simple solution would be starting first service in a new command process (in background) , wait for example two seconds and then start second service also in a new command process (in background) before exiting current command process. 一个非常简单的解决方案是在新命令流程中(后台)启动第一服务,等待两秒钟,然后在新命令流程中(后台)启动第二服务,然后退出当前命令流程。
@echo off
start "Service 1" /B java.exe -jar service1.jar --spring.profiles.active=local
%SystemRoot%\System32\timeout.exe /T 2 /NOBREAK >nul
start "Service 2" /B java.exe -jar service2.jar --spring.profiles.active=local
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully. 为了了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面。
echo /?
start /?
timeout /?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.