简体   繁体   English

如何使用Windows批处理文件一个一启动Spring Boot jar文件?

[英]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.

相关问题 使用Java Spring Boot,如何将依赖项,源代码和Spring合并到一个Jar文件中 - Using Java Spring Boot, How do I combine the dependencies, my source code, and Spring into one Jar file 从批处理文件一个接一个地运行多个jar文件 - Run multiple jar files from batch file one by one Spring 引导批处理 MultiResourceItemReader:如何使用 @Value() 从 jar 位置读取文件 - Spring boot batch MultiResourceItemReader: how to read files using @Value() from jar location 使用nohup通过终端启动Spring Boot Jar文件 - Spring Boot Jar file start by terminal using nohup 如何使用 Linux 上的外部 *.ampl 启动可执行 spring 启动 jar 文件? - How to start executable spring boot jar file with external *.ampl on Linux? 如何将JNI挂钩添加到Spring Boot生成的一个jar中 - How to add JNI hooks to one jar generated by Spring Boot 在一个 jar 中打包两个 spring boot 应用程序 - Pack two spring boot application in one jar spring 批量读取多个文件并写入一个文件 - spring batch read from multiple files and write to one file 如何使用jenkins将spring boot jar文件部署到EC2? - How to deploy spring boot jar file to EC2 using jenkins? 如何使用 gradle 从 spring 引导应用程序生成 jar 文件? - How to generate jar file from spring boot application using gradle?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM