文档章节

使用 kill 命令杀死 java进程,你用对了吗?

o
 osc_zoa3moe9
发布于 2019/12/07 11:45
字数 2320
阅读 7
收藏 0

精选30+云产品,助力企业轻松上云!>>>

<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>

    <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>

<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>

</div>

o
粉丝 1
博文 500
码字总数 0
作品 0
私信 提问
加载中
请先登录后再评论。
kill 命令在Java应用中使用注意事项

前言 我们都知道,kill在linux系统中是用于杀死进程。 kill pid [..] kill命令可将指定的信号发送给相应的进程或工作。 kill命令默认使用信号为15,用于结束进程或工作。如果进程或工作忽略此...

osc_d4gurmqk
04/16
6
0
Linux系统下如何优雅地关闭Java进程?

资料出处: http://www.sohu.com/a/329564560_700886       https://www.cnblogs.com/nuccch/p/10903162.html 前言   Linux系统下如何kill掉一个后台Java进程,相信童鞋们都知道如何操...

osc_tjdfnjyo
2019/12/21
8
0
Linux 下shell启动Java Main函数脚本

#!/bin/sh # #该脚本为Linux下启动java程序的通用脚本。即可以作为开机自启动service脚本被调用, #也可以作为启动java程序的独立脚本来使用。 #警告!!!:该脚本stop部分使用系统kill命令来强...

心月狐
2018/05/14
0
0
Java应用异常状态监测

云栖社区
2018/06/26
0
0
Java应用异常状态监测

原文链接:点击打开链接 摘要: 老板最近分派了一个任务,说线上客户在部署应用的时候发生了系统级别的OOM,触发了OOM Killer杀掉了应用,让我们解决这个问题。对于这个任务,我从如下几点开...

qq_42154484
03/31
0
0

没有更多内容

加载失败,请刷新页面

加载更多

智慧旅游背景下的景区新模式建设方案研讨

景区经营权管理方法实践、县域旅游基本模式以及智慧旅游一直是业内探究的关键。学术界对景区经营权出让探究比较多,如阎友兵探究了根据实物期权基础理论的景区经营权出让期限制度分配;郭淳凡...

osc_rezr8v4k
16分钟前
5
0
详解区块链是什么!KiBiEx加密货币交易所~

区块链是一个信息技术领域的术语。从本质上讲,它是一个共享数据库,存储于其中的数据或信息,具有“不可伪造”“全程留痕”“可以追溯”“公开透明”“集体维护”等特征。基于这些特征,区块...

osc_3361hjxk
17分钟前
8
0
详解区块链是什么!KiBiEx加密货币交易所~

区块链是一个信息技术领域的术语。从本质上讲,它是一个共享数据库,存储于其中的数据或信息,具有“不可伪造”“全程留痕”“可以追溯”“公开透明”“集体维护”等特征。基于这些特征,区块...

osc_3ytpwpyb
19分钟前
4
0
详解区块链是什么!KiBiEx加密货币交易所~

区块链是一个信息技术领域的术语。从本质上讲,它是一个共享数据库,存储于其中的数据或信息,具有“不可伪造”“全程留痕”“可以追溯”“公开透明”“集体维护”等特征。基于这些特征,区块...

osc_x4ot1joy
21分钟前
3
0
如何强制gradle重新下载依赖项? - How can I force gradle to redownload dependencies?

问题: 如何告诉gradle从存储库重新下载依赖项? 解决方案: 参考一: https://stackoom.com/question/uute/如何强制gradle重新下载依赖项 参考二: https://oldbug.net/q/uute/How-can-I-fo...

fyin1314
24分钟前
5
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部