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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 06:41:13  来源:igfitidea点击:

Ubuntu Java: Find a specific program's pid and kill the program

javalinuxubuntukillpid

提问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 foobarfor getting the pid, then issuing the killcommand against it, alternatively you might want to use pkillfoobar, in both cases foobarbeing the name of the app you want to terminate.

您可能会尝试ps -aux | grep foobar获取 pid,然后kill针对它发出命令,或者您可能想要使用,在这两种情况下都是您要终止的应用程序的名称。pkillfoobarfoobar

回答by millimoose

You'll probably have to invoke the psand killUnix 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调用pskillUnix 命令并抓取它们的输出。我不认为 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 jpswhich 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 pidofto 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 -auxthrows an syntax error, where ps auxworks.

在 Ubuntu 中,ps -aux抛出一个语法错误,在那里ps aux工作。

the output is piped to awkwhich 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}' | bashbut 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

Linux下查找java进程的pid

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();
    }
}
}