簡體   English   中英

"終止 mvn spring-boot:run 不會停止 tomcat"

[英]Terminating mvn spring-boot:run doesn't stop tomcat

我可以使用mvn spring-boot<\/code> , 文檔<\/a>中提到優雅地退出應用程序點擊ctrl-c<\/code> 。

Terminate batch job (Y/N)? Y

在 Windows 7 上運行的 1.1.9 版上,它仍然發生在我身上。

所以在按下 Ctrl C 之后,殺死后台 java 的最快方法是 .

找到java PID

     c:\>netstat -ano | find "8080"
     TCP    0.0.0.0:8080           0.0.0.0:0              LISTENING       1196
     TCP    [::]:8080              [::]:0                 LISTENING       1196
     c:\>taskkill /F /PID 1196
     SUCCESS: The process with PID 1196 has been terminated.

我在上面的例子中假設你在 http 端口 8080 上運行

對於 Mac 用戶:

     $lsof -i :8080
     COMMAND   PID   USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
     java    SOME_PID user  417u  IPv6 0xa4b3be242c249a27      0t0  TCP *:name (LISTEN)
     kill -9 SOME_PID

這是我在 Mac 上所做的:

kill `lsof -i -n -P | grep TCP | grep 8080 | tr -s " " "\n" | sed -n 2p`

它使用 8080 找到 PID 並殺死它。

編輯:在 ubuntu 中,使用命令:

fuser -k 8080/tcp

殺死進程。 我正在使用 spring-boot 1.3.0-Build-snapshot 並且問題仍然存在。 我的 SO 是 Ubuntu,問題發生在從 Eclipse 或控制台運行時。 關於這個問題的討論是here 這似乎是 spring-boot 庫的一個問題,至少與 tomcat 庫有關。 我也在排隊進行此修復。 不幸的是,這是我們嘗試使用尖端技術直到它們成熟為止所付出的代價。

對於那些使用 Eclipse 的人來說,這是迄今為止最好的答案:

打開“運行配置”,編輯您為項目定義的 Maven 啟動,然后在“JRE”選項卡下,將-Dfork=false添加到 VM 參數文本區域。

然后當您點擊紅色停止按鈕時,tomcat 服務器將停止並且您的端口將被釋放。

答案來自 jordihs 在 Github 上的帖子

在 IDEA 中,您必須在再次運行之前停止進程 該命令將關閉嵌入式 Tomcat。

我知道我回答得太晚了,但可能有人會得到幫助。 STS 用戶可以使用Relaunch按鈕而不是Run來確保關閉任何現有實例。
以供參考 :
https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-running-your-application.html

這只發生在 Windows 中。 https://github.com/spring-projects/spring-boot/issues/773

更新:現在應該修復。

是的,這是正確的,當您使用 spring-boot-run 進行 maven 安裝並且在應用程序停止后,tomcat 端口仍會偵聽。

我正在為那些遇到此問題的端口/地址已在使用的人附上屏幕截圖。 您需要做的是在再次運行 spring boot 應用程序之前,只需轉到您的 Windows 任務管理器並結束名為“Java(TM) Platform SE 二進制文件”的進程並運行您的啟動應用程序,您的端口將不受該進程的影響而您不會再遇到這個問題。(在運行應用程序后,您不必執行此操作在此處輸入圖片說明 第一次)它為我創造了奇跡。 希望它有助於美好的一天

我遇到了同樣的問題,但有一種更好、更簡單的方法來運行沒有這個問題的 Spring boot 應用程序。

運行方式 -> Java 應用程序

更新:此外,如果您使用 Spring STS(可用於 VS Code、IntelliJ、Eclipse...),您有機會從引導儀表板管理您的應用程序。 它真的很有用。

在此處輸入圖片說明

我在 Mac 上從 netbeans 8.1 運行 spring boot 應用程序時遇到了這個問題。 當我在 netbeans 中點擊紅色方形按鈕時,java 進程沒有終止,所以當我重新啟動應用程序時,我總是得到“綁定異常,地址已經在使用”的東西。 這是已知的錯誤。

解決方案是添加 spring-boot:run命令來運行項目的目標......

...而且如果你得到“在當前項目和插件組中沒有找到前綴‘spring-boot’的插件”,你可能需要將它添加到依賴項中:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.5.RELEASE</version>
</parent>

<repositories>
    <repository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </pluginRepository>
</pluginRepositories>

我在 Mac 上的 eclipse 中運行 SpringBoot 應用程序時遇到了同樣的問題。 我必須手動找到所有使用 8080 的 PID 並殺死它們。 但是,幸運的是,我意識到我們可以直接從 Eclipse 的“控制台”視圖中殺死那個 tomcat 實例,方法是點擊停止(紅色方塊圖標)按鈕並再次運行 Spring Boot 應用程序。

如果您正確配置了 spring-boot,您可以通過調用curl -v -X POST http://127.0.0.1:8091/shutdown導致關機。

為了支持這一點,您必須更新您的應用程序屬性。 這在https://stackoverflow.com/a/26563344/58794 中有部分描述。

通過添加以下內容更新 application.yml:

management:
  security:
    enabled: false
endpoints:
  shutdown:
    enabled: true

或通過添加以下內容來更新 application.properties:

management.security.enabled=false
endpoints.shutdown.enabled=true

一次叫

$ curl -v -X POST http://127.0.0.1:8091/shutdown
* STATE: INIT => CONNECT handle 0x600057990; line 1423 (connection #-5000)
* Added connection 0. The cache now contains 1 members
*   Trying 127.0.0.1...
* TCP_NODELAY set
* STATE: CONNECT => WAITCONNECT handle 0x600057990; line 1475 (connection #0)
* Connected to 127.0.0.1 (127.0.0.1) port 8091 (#0)
* STATE: WAITCONNECT => SENDPROTOCONNECT handle 0x600057990; line 1592 (connection #0)
* Marked for [keep alive]: HTTP default
* STATE: SENDPROTOCONNECT => DO handle 0x600057990; line 1610 (connection #0)
> POST /shutdown HTTP/1.1
> Host: 127.0.0.1:8091
> User-Agent: curl/7.56.1
> Accept: */*
>
* STATE: DO => DO_DONE handle 0x600057990; line 1689 (connection #0)
* STATE: DO_DONE => WAITPERFORM handle 0x600057990; line 1814 (connection #0)
* STATE: WAITPERFORM => PERFORM handle 0x600057990; line 1824 (connection #0)
* HTTP 1.1 or later with persistent connection, pipelining supported
< HTTP/1.1 200
< X-Application-Context: application:h2:8091
< Content-Type: application/vnd.spring-boot.actuator.v1+json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Fri, 22 Dec 2017 07:01:04 GMT
<
* STATE: PERFORM => DONE handle 0x600057990; line 1993 (connection #0)
* multi_done
* Connection #0 to host 127.0.0.1 left intact
* Expire cleared
{"message":"Shutting down, bye..."}

容器將關閉

o.s.c.support.DefaultLifecycleProcessor  : Stopping beans in phase 0
o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown
j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'

但是如果你從mvn spring-boot:run你可能會得到:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 35.613 s
[INFO] Finished at: 2017-12-22T02:01:05-05:00
[INFO] Final Memory: 25M/577M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.7.RELEASE:run (default-cli) on project PROJECTNAME: Could not exec java: Application finished with exit code: 1 -> [Help 1]

如果您沒有management.security.enabled=false您可能會看到以下錯誤:

$ curl -v -X POST http://127.0.0.1:8091/shutdown
> POST /shutdown HTTP/1.1
> Host: 127.0.0.1:8091
> User-Agent: curl/7.56.1
> Accept: */*
>
< HTTP/1.1 401
< X-Application-Context: application:h2:8091
< Content-Type: application/vnd.spring-boot.actuator.v1+json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Fri, 22 Dec 2017 06:56:19 GMT
<
{"timestamp":1513925779265,"status":401,"error":"Unauthorized","message":"Full authentication is required to access this resource.","path":"/shutdown"}

如果您沒有endpoints.shutdown.enabled=true您將看到:

$ curl -v -X POST http://127.0.0.1:8091/shutdown
> POST /shutdown HTTP/1.1
> Host: 127.0.0.1:8091
> User-Agent: curl/7.56.1
> Accept: */*
>
< HTTP/1.1 404
< X-Application-Context: application:h2:8091
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Fri, 22 Dec 2017 06:58:52 GMT
<
{"timestamp":1513925932345,"status":404,"error":"Not Found","message":"No message available","path":"/shutdown"}

如果您嘗試使用 GET 而不是 POST ,則會出現此錯誤:

$ curl -v http://127.0.0.1:8091/shutdown
> GET /shutdown HTTP/1.1
> Host: 127.0.0.1:8091
> User-Agent: curl/7.56.1
> Accept: */*
>
< HTTP/1.1 405
< X-Application-Context: application:h2:8091
< Allow: POST
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Fri, 22 Dec 2017 06:54:12 GMT
<
{"timestamp":1513925652827,"status":405,"error":"Method Not Allowed","exception":"org.springframework.web.HttpRequestMethodNotSupportedException","message":"Request method 'GET' not supported","path":"/shutdown"}

這個解決方案適用於 Spring Boot v1.x,現在適用於 v2.x:

hangingJavaProcessToStop=`jps | grep Application | awk '{print $1}'`
echo "hangingJavaProcessToStop: $hangingJavaProcessToStop"
kill -9 $hangingJavaProcessToStop

通過這種方式,您可以專門殺死 Spring Boot 的Application Java 進程,而不是一次殺死所有 Java 進程。 我假設如果您的 Spring Boot“應用程序”(包含類的main方法)的調用方式不同,您必須使用它的名稱而不是Application

我正在使用WSL/Debian在我的Windows機器上運行上面的代碼片段。 我沒有使用 PowerShell 或 Windows 的命令行。

如果您使用的是 NetBeans,您可以通過單擊右下角顯示運行(項目名稱)的十字圖標來停止服務器或進程。 在此處輸入圖片說明

對於 Powershell,將以下代碼放在以 '.ps1' 結尾的文件中$processes = (get-NetTCPConnection| ? {$_.LocalPort -eq "8080"}).OwningProcess foreach ($process in $processes) {Get-Process -PID $process | Stop-Process -Force} $processes = (get-NetTCPConnection| ? {$_.LocalPort -eq "8080"}).OwningProcess foreach ($process in $processes) {Get-Process -PID $process | Stop-Process -Force}

我們可以直接從 eclipse console視圖中殺死那個 tomcat 實例,通過點擊停止(紅色方塊圖標)按鈕和運行圖標旁邊的紅色按鈕,再次運行 spring boot 應用程序。

我在 Windows 10 中遇到了同樣的問題,我在 Cygwin 終端中運行mvn spring-boot:run 我什至無法使用ps -ef在 Cygwin 中找到 Tomcat 進程; 我不得不使用netstat -ao在 PowerShell 中搜索該進程。

在 PowerShell 中使用mvn spring-boot:run對我來說工作正常。

我最近在 Mac OS 上遇到了同樣的問題,這似乎是在升級我的 Mac OS 版本后發生的。

使用 IntelliJ IDEA 作為 IDE 並安裝一個 spring boot,運行過程無法停止,因為端口仍然打開。 我每次都必須使用lsof -i:<running port>kill -9 <PID>手動殺死進程,煩人!

在此處輸入圖像描述

在eclipse的情況下::如果出現這個錯誤,你可以在eclipse上停止它,不需要從任務管理器中殺掉,請參考SS,這里所有正在運行的服務都存在,選擇一個並停止它們。 SS 日食錯誤<\/a>

"

按順序鍵入以下命令。

$ ps -ef | grep -i java
$ kill -9 3361

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM