Linux Ubuntu Java:查找特定程序的 pid 并终止该程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7762257/
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
Ubuntu Java: Find a specific program's pid and kill the program
提问by Abraham Miguel Espiritu
I'm trying to make an application that checks if this specific application is running, then kill the app after a specified amount of time. I'm planning to get the pid of the app. How can I get the pid of the app?
我正在尝试制作一个应用程序来检查此特定应用程序是否正在运行,然后在指定的时间后终止该应用程序。我打算获取应用程序的 pid。如何获取应用程序的pid?
Thanks
谢谢
采纳答案by Nano Taboada
You might try ps -aux | grep foobar
for getting the pid, then issuing the kill
command against it, alternatively you might want to use pkill
foobar
, in both cases foobar
being the name of the app you want to terminate.
您可能会尝试ps -aux | grep foobar
获取 pid,然后kill
针对它发出命令,或者您可能想要使用,在这两种情况下都是您要终止的应用程序的名称。pkill
foobar
foobar
回答by millimoose
You'll probably have to invoke the ps
and kill
Unix commands using Runtime.execand scrape their output. I don't think a Java program can easily inspect / access processes it didn't create.
您可能必须使用Runtime.exec调用ps
和kill
Unix 命令并抓取它们的输出。我不认为 Java 程序可以轻松检查/访问它没有创建的进程。
回答by mtrovo
If you're starting this application from script you can get the pid of the last process that was created in background by using the special variable $!
, this value you can save on a file for later use on your shutdown function.
如果您从脚本启动此应用程序,您可以使用特殊变量获取在后台创建的最后一个进程的 pid,$!
您可以将此值保存在文件中以供以后在关闭功能中使用。
Below is an example:
下面是一个例子:
nohup java -jar example.jar &
echo $! > application.pid
回答by Steve B.
pid's for java processes -e.g ps -aux | grep java | awk '{print $2}'
.
pid 用于 java 进程 - 例如 ps -aux | grep java | awk '{print $2}'
。
You can also call jps
which will give you a process list. A drawback of this is that it will only show you processes for the user issuing the jps command.
你也可以调用jps
它会给你一个进程列表。这样做的一个缺点是它只会向您显示发出 jps 命令的用户的进程。
回答by Kit Ho
you can use pidof
to get the pid of your application
你可以pidof
用来获取你的应用程序的pid
pidof <application name>
pidof <application name>
回答by matchew
ps aux | awk '/java/ {print "sleep 10; kill "$2}' | bash
ps aux | awk '/java/ {print "sleep 10; kill "$2}' | bash
in Ubuntu, ps -aux
throws an syntax error, where ps aux
works.
在 Ubuntu 中,ps -aux
抛出一个语法错误,在那里ps aux
工作。
the output is piped to awk
which matches lines with java and sleeps for 10seconds and then kills the program with the pId. notice the pipe to bash. Feel free to specify however long you want, or to call what ever other calls you feel appropriate. I notice most of the other answers neglected to catch the 'after a certain amount of time' part of the quesiton.
输出通过管道传输到awk
与 java 匹配的行并休眠 10 秒,然后使用 pId 终止程序。注意要 bash 的管道。随意指定您想要的时间长度,或者拨打您认为合适的任何其他电话。我注意到大多数其他答案都忽略了问题的“一段时间后”部分。
you could also accomplish this by calling pidof java | awk '{print "sleep 10; kill "$1}' | bash
but the choice is yours. I generally use ps aux.
您也可以通过致电来完成此操作,pidof java | awk '{print "sleep 10; kill "$1}' | bash
但选择权在您。我一般用ps aux。
回答by Shweta B. Patil
Find the pid of a java process under Linux
you can refer to this link...it tell you about finding a pid of specific class name
您可以参考此链接...它告诉您有关查找特定类名的 pid
回答by freddi3k
How about this then?
那这个怎么样?
public static void killAll(String process) {
try {
Vector<String> commands = new Vector<String>();
commands.add("pidof");
commands.add(process);
ProcessBuilder pb = new ProcessBuilder(commands);
Process pr = pb.start();
pr.waitFor();
if (pr.exitValue() != 0) return;
BufferedReader outReader = new BufferedReader(new InputStreamReader(pr.getInputStream()));
for (String pid : outReader.readLine().trim().split(" ")) {
log.info("Killing pid: "+pid);
Runtime.getRuntime().exec("kill " + pid).waitFor();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}