![](/img/trans.png)
[英]Starting Spring Boot as a foreground process from within a bash script
[英]Starting Spring boot applications from script
使用普通的 spring mvn 命令,我可以從命令行啟動一個 spring 啟動應用程序,並用 Control+c 終止它。 然而,我已經創建了一堆服務,稍后我將對其進行 dockerize。 現在它們是由 mvn 生成的普通 java jar 文件。 我如何使用python腳本或Bash腳本將它們一一啟動,然后使用腳本終止它們。 有什么方法可以讓我啟動它並且腳本不會阻止並且應用程序會有一個名稱,我以后可以用它來停止應用程序?
我會按照文檔將Spring-Boot
應用程序安裝為Unix/Linux
服務。
您所要做的就是將此依賴項添加到您的pom.xml
:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
添加插件后,您應該安裝並創建指向您的應用程序的符號鏈接( 文檔的確切部分):
假設您在
/var/myapp
中安裝了 Spring Boot 應用程序,要將 Spring Boot 應用程序安裝為 init.d 服務,只需創建一個符號鏈接:
$ sudo ln -s /var/myapp/myapp.jar /etc/init.d/myapp
安裝后,您可以按照通常的方式啟動和停止服務。 例如,在基於 Debian 的系統上:
$ service myapp start
然后您就可以創建一個 bash 腳本以干凈的方式start
、 stop
或restart
您的應用程序。
您可以使用腳本來實現這一點。 例如, startup.sh
可能看起來像這樣。 它將啟動應用程序並將進程 ID 寫入/path/to/app/pid.file
#!/bin/bash
nohup java -jar /path/to/app/hello-world.jar > /path/to/log.txt 2>&1 &
echo $! > /path/to/app/pid.file
一個shutdown.sh
可能看起來像這樣。
#!/bin/bash
kill $(cat /path/to/app/pid.file)
您可以在我的帖子中找到更多詳細信息。 https://springhow.com/start-stop-scripts-for-spring-boot-applications/
java -jar service1.jar &
pkill -f service1.jar
pkill
將終止包含所提供名稱的所有進程。 請注意,您的關鍵字只能標識您的進程,以免錯誤地終止其他進程。
這個腳本可以很容易地自動查找最新版本的 jar 文件:
https://github.com/tyrion9/spring-boot-startup-script
./bootstrap.sh start
./bootstrap.sh stop
./bootstrap.sh restart
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.