优雅地停止由maven-antrun-plugin启动的Java进程 [英] Gracefully stopping a java process started by maven-antrun-plugin
问题描述
这个问题是昨天回答这个问题的结果
This question is a result of an answer to this question from yesterday
因此,在上述问题中得到答复,我现在有了一个maven-antrun-plugin,它可以分叉子进程并使用类似这样的配置来运行我的java appserver-
So as answered in the above question I now have a maven-antrun-plugin that forks a child process and runs my java appserver using a configuration like this -
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase> verify </phase>
<configuration>
<target>
<property name="runtime_classpath" refid="maven.runtime.classpath" />
<exec executable="java" spawn="true">
<arg value="-classpath"/>
<arg value="${runtime_classpath}"/>
<arg value="somepackage.AppServer"/>
</exec>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
以上配置可作为后台进程顺利启动我的应用服务器.
The above configuration smoothly starts my appserver as a background process.
现在我的问题是,有没有一种简便的方法可以定位此过程并在构建完成后根据需要停止该过程.
Now my question is if there is an easy way I can locate this process and stop it if I need after I start it through my build.
推荐答案
You can use the jps
utility that is bundled inside the JDK to get the process ID of a running Java executable:
jps
工具列出了目标系统上已检测到的HotSpot Java虚拟机(JVM).该工具仅限于报告有关其具有访问权限的JVM的信息.
The
jps
tool lists the instrumented HotSpot Java Virtual Machines (JVMs) on the target system. The tool is limited to reporting information on JVMs for which it has the access permissions.
Then, when we have retrieved the process ID, we can kill it using taskkill
on Windows or kill
or Unix system.
这是maven-antrun-plugin
的示例配置.它声明jps
可执行文件,并使用process.pid中重定向其调用结果. > <redirector>
属性.结果用 <outputfilterchain>
进行过滤,以便仅对应于可执行文件的行AppServer
被保留. jps
输出的格式为[PID] [NAME]
,因此该名称随后用 <replacestring>
;这样,我们只保留PID.最后,取决于操作系统,有两种exec
配置.
This would be a sample configuration of the maven-antrun-plugin
. It declares the jps
executable and redirect the result of its invocation in the property process.pid
with the <redirector>
attribute. The result is filtered with <outputfilterchain>
so that only the lines corresponding to the executable AppServer
are kept. jps
output is in the form [PID] [NAME]
so the name is then removed with <replacestring>
; this way, we only keep the PID. Finally, there are two exec
configuration depending on the OS.
<configuration>
<target>
<property name="runtime_classpath" refid="maven.runtime.classpath" />
<exec executable="java" spawn="true">
<arg value="-classpath" />
<arg value="${runtime_classpath}" />
<arg value="somepackage.AppServer" />
</exec>
<exec executable="${JAVA_HOME}/bin/jps">
<arg value="-l" />
<redirector outputproperty="process.pid">
<outputfilterchain>
<linecontains>
<contains value="somepackage.AppServer" />
</linecontains>
<replacestring from=" somepackage.AppServer" />
</outputfilterchain>
</redirector>
</exec>
<exec executable="taskkill" osfamily="winnt">
<arg value="/PID" />
<arg value="${process.pid}" />
</exec>
<exec executable="kill" osfamily="unix">
<arg value="-15" />
<arg value="${process.pid}" />
</exec>
</target>
</configuration>
由于您提到的是优美",所以我在Unix上使用了-15
选项,而在Windows中没有包含/F
选项.如果要强制退出,可以在Unix系统上使用kill -9
并在Windows上添加/F
选项.
Since you mentioned "gracefully", I used the -15
option on Unix and didn't include the /F
option for Windows. If you want to force the exit, you can use kill -9
on the Unix system and add the /F
option on Windows.
这篇关于优雅地停止由maven-antrun-plugin启动的Java进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!