linux脚本杀死java进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13711855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
linux script to kill java process
提问by d-man
I want linux script to kill java program running on console.
我想要 linux 脚本杀死在控制台上运行的 java 程序。
Following is the process running as jar.
以下是作为 jar 运行的进程。
[rapp@s1-dlap0 ~]$ ps -ef |grep java
rapp 9473 1 0 15:03 pts/1 00:00:15 java -jar wskInterface-0.0.1-SNAPSHOT-jar-with-dependencies.jar
rapp 10177 8995 0 16:00 pts/1 00:00:00 grep java
[rapp@s1-dlap0 ~]$
采纳答案by anubhava
You can simply use pkill -f
like this:
你可以简单地pkill -f
像这样使用:
pkill -f 'java -jar'
EDIT:To kill a particular java process running your specific jar use this regex based pkill command:
编辑:要终止运行特定 jar 的特定 java 进程,请使用此基于正则表达式的 pkill 命令:
pkill -f 'java.*lnwskInterface'
回答by lynks
If you just want to kill any/all java processes, then all you need is;
如果您只想杀死任何/所有 java 进程,那么您只需要;
killall java
If, however, you want to kill the wskInterfaceprocess in particular, then you're most of the way there, you just need to strip out the process id;
但是,如果您特别想终止 wskInterface进程,那么您已经完成了大部分工作,您只需要去掉进程 ID;
PID=`ps -ef | grep wskInterface | awk '{ print }'`
kill -9 $PID
Should do it, there is probably an easier way though...
应该这样做,但可能有更简单的方法......
回答by Pawan
Use jpsto list running java processes. The command returns the process id along with the main class. You can use kill command to kill the process with the returned id or use following one liner script.
使用jps列出正在运行的 java 进程。该命令返回进程 ID 和主类。您可以使用 kill 命令使用返回的 id 终止进程,或者使用以下一个 liner 脚本。
kill $(jps | grep <MainClass> | awk '{print }')
MainClass is a class in your running java program which contains the main method.
MainClass 是您正在运行的 java 程序中的一个类,其中包含 main 方法。
回答by Jilles van Gurp
pkill -f for whatever reason does not work for me. Whatever that does, it seems very finicky about actually grepping through what ps aux shows me clearly is there.
pkill -f 无论出于何种原因对我都不起作用。不管它做什么,实际上通过 ps aux 向我展示的内容似乎非常挑剔。
After an afternoon of swearing I went for putting the following in my start script:
经过一个下午的咒骂,我开始将以下内容放入我的开始脚本中:
(ps aux | grep -v -e 'grep ' | grep MainApp | tr -s " " | cut -d " " -f 2 | xargs kill -9 ) || true
(ps aux | grep -v -e 'grep ' | grep MainApp | tr -s " " | cut -d " " -f 2 | xargs kill -9 ) || true