使用 kill 命令殺死 java進程,你用對了嗎?

<div id="cnblogs_post_body" class="blogpost-body cnblogs-markdown"> <article class="_2rhmJa"> <p>在本地調試agent相關功能,須要常常性的殺掉Java進程,驗證一些極端狀況。</p> <p>每次都是本能執行以下步驟</p> <ul> <li>jps</li> <li>kill -9 &lt;pid&gt;</li> <li>reboot</li> </ul> <p>有一次驗證,發現代碼中添加的<code>ShutdownHook</code>沒有生效,難道和<code>kill</code>命令後面的數字有關?</p> <p>通過一番查閱,後面的數字表明的是具體信號,<code>kill</code>命令可將指定的信號發送給相應的進程,linux中常見的信號以下:</p> <ul> <li>1 SIGHUP 掛起進程</li> <li>2 SIGINT 終止進程</li> <li>3 SIGGQUIT 中止進程</li> <li>9 SIGKILL 無條件終止進程</li> <li>15 SIGTERM 儘量終止進程</li> <li>17 SIGSTOP 無條件中止進程,但不是終止</li> <li>18 SIGTSTP 中止或者暫停進程,但不終止進程</li> <li>19 SIGCONT 繼續運行中止的進程</li> </ul> <p><code>kill</code>命令默認狀況使用15,下面咱們驗證下使用不一樣信號,有什麼不一樣的表現。</p> <h3>建立一個springBoot應用</h3> <p>啓動類以下,添加了一個鉤子函數,當進程關閉時,將會調用該鉤子函數。</p> <div class="_2Uzcx_"><pre class="line-numbers language-java"><code class="language-java hljs"><span class="token annotation punctuation"><span class="hljs-meta"><span class="hljs-meta"><span class="hljs-meta">@SpringBootApplication</span></span></span></span> <span class="token keyword"><span class="hljs-keyword"><span class="hljs-keyword"><span class="hljs-keyword">public</span></span></span></span> <span class="token keyword"><span class="hljs-class"><span class="hljs-keyword"><span class="hljs-class"><span class="hljs-keyword"><span class="hljs-class"><span class="hljs-keyword">class</span></span></span></span></span></span></span><span class="hljs-class"><span class="hljs-class"><span class="hljs-class"> </span></span></span><span class="token class-name"><span class="hljs-class"><span class="hljs-title"><span class="hljs-class"><span class="hljs-title"><span class="hljs-class"><span class="hljs-title">Server</span></span></span></span></span></span></span><span class="hljs-class"><span class="hljs-class"><span class="hljs-class"> </span></span></span><span class="token punctuation">{</span> <span class="token keyword"><span class="hljs-function"><span class="hljs-keyword"><span class="hljs-function"><span class="hljs-keyword"><span class="hljs-function"><span class="hljs-keyword">public</span></span></span></span></span></span></span><span class="hljs-function"><span class="hljs-function"><span class="hljs-function"> </span></span></span><span class="token keyword"><span class="hljs-function"><span class="hljs-keyword"><span class="hljs-function"><span class="hljs-keyword"><span class="hljs-function"><span class="hljs-keyword">static</span></span></span></span></span></span></span><span class="hljs-function"><span class="hljs-function"><span class="hljs-function"> </span></span></span><span class="token keyword"><span class="hljs-function"><span class="hljs-keyword"><span class="hljs-function"><span class="hljs-keyword"><span class="hljs-function"><span class="hljs-keyword">void</span></span></span></span></span></span></span><span class="hljs-function"><span class="hljs-function"><span class="hljs-function"> </span></span></span><span class="token function"><span class="hljs-function"><span class="hljs-title"><span class="hljs-function"><span class="hljs-title"><span class="hljs-function"><span class="hljs-title">main</span></span></span></span></span></span></span><span class="token punctuation"><span class="hljs-function"><span class="hljs-params"><span class="hljs-function"><span class="hljs-params"><span class="hljs-function"><span class="hljs-params">(</span></span></span></span></span></span></span><span class="token class-name"><span class="hljs-function"><span class="hljs-params"><span class="hljs-function"><span class="hljs-params"><span class="hljs-function"><span class="hljs-params">String</span></span></span></span></span></span></span><span class="token punctuation"><span class="hljs-function"><span class="hljs-params"><span class="hljs-function"><span class="hljs-params"><span class="hljs-function"><span class="hljs-params">[</span></span></span></span></span></span></span><span class="token punctuation"><span class="hljs-function"><span class="hljs-params"><span class="hljs-function"><span class="hljs-params"><span class="hljs-function"><span class="hljs-params">]</span></span></span></span></span></span></span><span class="hljs-function"><span class="hljs-params"><span class="hljs-function"><span class="hljs-params"><span class="hljs-function"><span class="hljs-params"> args</span></span></span></span></span></span><span class="token punctuation"><span class="hljs-function"><span class="hljs-params"><span class="hljs-function"><span class="hljs-params"><span class="hljs-function"><span class="hljs-params">)</span></span></span></span></span></span></span><span class="hljs-function"><span class="hljs-function"><span class="hljs-function"> </span></span></span><span class="token punctuation">{</span> <span class="token class-name">SpringApplication</span><span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token class-name">Server</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>java

<span class="token class-name">Runtime</span><span class="token punctuation">.</span><span class="token function">getRuntime</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">addShutdownHook</span><span class="token punctuation">(</span><span class="token keyword"><span class="hljs-keyword"><span class="hljs-keyword"><span class="hljs-keyword">new</span></span></span></span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token annotation punctuation"><span class="hljs-meta"><span class="hljs-meta"><span class="hljs-meta">@Override</span></span></span></span>
        <span class="token keyword"><span class="hljs-function"><span class="hljs-keyword"><span class="hljs-function"><span class="hljs-keyword"><span class="hljs-function"><span class="hljs-keyword">public</span></span></span></span></span></span></span><span class="hljs-function"><span class="hljs-function"><span class="hljs-function"> </span></span></span><span class="token keyword"><span class="hljs-function"><span class="hljs-keyword"><span class="hljs-function"><span class="hljs-keyword"><span class="hljs-function"><span class="hljs-keyword">void</span></span></span></span></span></span></span><span class="hljs-function"><span class="hljs-function"><span class="hljs-function"> </span></span></span><span class="token function"><span class="hljs-function"><span class="hljs-title"><span class="hljs-function"><span class="hljs-title"><span class="hljs-function"><span class="hljs-title">run</span></span></span></span></span></span></span><span class="token punctuation"><span class="hljs-function"><span class="hljs-params"><span class="hljs-function"><span class="hljs-params"><span class="hljs-function"><span class="hljs-params">(</span></span></span></span></span></span></span><span class="token punctuation"><span class="hljs-function"><span class="hljs-params"><span class="hljs-function"><span class="hljs-params"><span class="hljs-function"><span class="hljs-params">)</span></span></span></span></span></span></span><span class="hljs-function"><span class="hljs-function"><span class="hljs-function"> </span></span></span><span class="token punctuation">{</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string"><span class="hljs-string"><span class="hljs-string"><span class="hljs-string">"do ShutdownHook.......... "</span></span></span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token punctuation">}</span> <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>linux

<p>你能夠經過</p> <div class="_2Uzcx_"><pre class="line-numbers language-ruby"><code class="language-ruby hljs">java <span class="token operator">-</span>jar <span class="token operator">~</span><span class="token operator"><span class="hljs-regexp"><span class="hljs-regexp"><span class="hljs-regexp">/</span></span></span></span><span class="hljs-regexp"><span class="hljs-regexp"><span class="hljs-regexp">project</span></span></span><span class="token operator"><span class="hljs-regexp"><span class="hljs-regexp"><span class="hljs-regexp">/</span></span></span></span><span class="hljs-regexp"><span class="hljs-regexp"><span class="hljs-regexp">web</span></span></span><span class="token operator"><span class="hljs-regexp"><span class="hljs-regexp"><span class="hljs-regexp">/</span></span></span></span><span class="hljs-regexp"><span class="hljs-regexp"><span class="hljs-regexp">target</span></span></span><span class="token operator"><span class="hljs-regexp"><span class="hljs-regexp"><span class="hljs-regexp">/</span></span></span></span><span class="hljs-regexp"><span class="hljs-regexp"><span class="hljs-regexp">demo</span></span></span><span class="token operator">-</span><span class="token number"><span class="hljs-number"><span class="hljs-number"><span class="hljs-number">1.0</span></span></span></span><span class="token punctuation">.</span>jar <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div> <p>也能夠加上nohup + &amp;啓動</p> <div class="_2Uzcx_"><pre class="line-numbers language-ruby"><code class="language-ruby hljs">nohup java <span class="token operator">-</span>jar <span class="token operator">~</span><span class="token operator"><span class="hljs-regexp"><span class="hljs-regexp"><span class="hljs-regexp">/</span></span></span></span><span class="hljs-regexp"><span class="hljs-regexp"><span class="hljs-regexp">project</span></span></span><span class="token operator"><span class="hljs-regexp"><span class="hljs-regexp"><span class="hljs-regexp">/</span></span></span></span><span class="hljs-regexp"><span class="hljs-regexp"><span class="hljs-regexp">web</span></span></span><span class="token operator"><span class="hljs-regexp"><span class="hljs-regexp"><span class="hljs-regexp">/</span></span></span></span><span class="hljs-regexp"><span class="hljs-regexp"><span class="hljs-regexp">target</span></span></span><span class="token operator"><span class="hljs-regexp"><span class="hljs-regexp"><span class="hljs-regexp">/</span></span></span></span><span class="hljs-regexp"><span class="hljs-regexp"><span class="hljs-regexp">demo</span></span></span><span class="token operator">-</span><span class="token number"><span class="hljs-number"><span class="hljs-number"><span class="hljs-number">1.0</span></span></span></span><span class="token punctuation">.</span>jar <span class="token operator">&amp;</span> <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div> <p>經過後者啓動,能夠看到啓動所在的目錄多了一個文件nohup.out,該文件記錄了應用啓動運行過程當中的日誌。</p> <p>&amp;表示之後臺方式運行應用。但若是退出關閉啓動的控制檯,進程將會中止。</p> <p>nohup + &amp;也是之後臺方式運行應用,可是退出關閉啓動的控制檯,進程不會中止,且進程日誌將會輸出到nohup.out中。</p> <h3>kill -3</h3> <p>經過執行jps 拿到對應的pid</p> <div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 324px; background-color: transparent;"> <div class="image-view" data-width="848" data-height="324"><img data-original-src="//upload-images.jianshu.io/upload_images/2184951-b0864755f1ab57dd.png" data-original-width="848" data-original-height="324" data-original-format="image/png" data-original-filesize="35728" data-image-index="1" style="cursor: zoom-in;" class="" src="https://img2018.cnblogs.com/blog/1112483/201912/1112483-20191207114944159-1513525918.png"></div> </div> <div class="image-caption"></div> </div> <p>並執行 <code>kill -3 5085</code>,驚奇的發現,Java進程並無被殺掉,而是打印了一堆線程信息。</p> <div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 620px; background-color: transparent;"> <div class="image-view" data-width="2382" data-height="620"><img data-original-src="//upload-images.jianshu.io/upload_images/2184951-a653a4cb75e68549.png" data-original-width="2382" data-original-height="620" data-original-format="image/png" data-original-filesize="252139" data-image-index="2" style="cursor: zoom-in;" class="" src="https://img2018.cnblogs.com/blog/1112483/201912/1112483-20191207115036298-489949244.png"></div> </div> <div class="image-caption"></div> </div> <h3>kill -9</h3> <p>上一步的 <code>kill -3</code> 並無成功的把進程殺掉,咱們繼續使用以前的pid。<br> 此次執行 <code>kill -9 5085</code></p> <div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 92px; background-color: transparent;"> <div class="image-view" data-width="1038" data-height="92"><img data-original-src="//upload-images.jianshu.io/upload_images/2184951-07b4549bd2e9f57c.png" data-original-width="1038" data-original-height="92" data-original-format="image/png" data-original-filesize="15168" data-image-index="3" style="cursor: zoom-in;" class="" src="//upload-images.jianshu.io/upload_images/2184951-07b4549bd2e9f57c.png"></div> </div> <div class="image-caption"></div> </div> <p>執行完 -9,java進程消失了,只留下這麼一段話。</p> <h3>kill -15</h3> <p>最後,再試試 <code>kill -15</code>,猶豫Java進程已經被 -9 給kill了,須要從新啓動一次。</p> <div class="image-package"> <div class="image-container" style="max-width: 700px; max-height: 400px; background-color: transparent;"> <div class="image-view" data-width="1662" data-height="400"><img data-original-src="//upload-images.jianshu.io/upload_images/2184951-707f86b77a3f379e.png" data-original-width="1662" data-original-height="400" data-original-format="image/png" data-original-filesize="129902" data-image-index="4" style="cursor: zoom-in;" class="" src="https://img2018.cnblogs.com/blog/1112483/201912/1112483-20191207115056944-1890100615.png"></div> </div> <div class="image-caption"></div> </div> <p>這一次,它打印了鉤子函數中的信息,隨之進程也消失了。</p> <h3>總結</h3> <p>kill -3 &lt;pid&gt; 這玩意通常用不到,能夠打印當前進程的線程信息,可是不會關閉Java應用!<br> kill -9 &lt;pid&gt; 很暴力,不會調用鉤子函數ShutdownHook。<br> kill &lt;pid&gt; 也就是kill -15 &lt;pid&gt; 很柔和,將會調用鉤子函數ShutdownHook,通常ShutdownHook中會進行一些操做,好比保存數據,關閉鏈接等。</p> 原文地址:https://www.jianshu.com/p/77ca821e7151 </article>web

</div>spring

相關文章
相關標籤/搜索