Ubuntu Java:查找特定程序的 pid 并终止该程序

发布于 2024-12-09 12:28:22 字数 94 浏览 1 评论 0原文

我正在尝试创建一个应用程序来检查此特定应用程序是否正在运行,然后在指定的时间后终止该应用程序。我打算获取应用程序的 pid。如何获取应用程序的 pid?

谢谢

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?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

临风闻羌笛 2024-12-16 12:28:22

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

You might try ps -aux | grep foobar for getting the pid, then issuing the kill command against it, alternatively you might want to use pkillfoobar, in both cases foobar being the name of the app you want to terminate.

木槿暧夏七纪年 2024-12-16 12:28:22

java 进程的 pid -eg ps -aux | java | grep | awk '{print $2}'

您还可以调用jps,它会给您一个进程列表。这样做的缺点是它只会显示发出 jps 命令的用户的进程。

pid's for java processes -e.g 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.

二智少女 2024-12-16 12:28:22

您可以使用 pidof 获取应用程序的 pid

pidof

you can use pidof to get the pid of your application

pidof <application name>

牵你的手,一向走下去 2024-12-16 12:28:22

那这个怎么样?

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

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();
    }
}
}
蓝眼睛不忧郁 2024-12-16 12:28:22

如果您从脚本启动此应用程序,您可以使用特殊变量 $! 获取在后台创建的最后一个进程的 pid,您可以将该值保存在文件中以供以后使用你的关机功能。

下面是一个例子:

nohup java -jar example.jar &
echo $! > application.pid

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.

Below is an example:

nohup java -jar example.jar &
echo $! > application.pid
谷夏 2024-12-16 12:28:22

PS 辅助 | awk '/java/ {print "sleep 10; Kill "$2}' | Ubuntu 中的bash

中,ps -aux 会抛出语法错误,而 ps aux 则有效。

输出通过管道传输到 awk,它与 java 匹配行并休眠 10 秒,然后使用 pId 终止程序。注意要 bash 的管道。请随意指定您想要的时间长度,或者拨打您认为合适的其他电话。我注意到大多数其他答案都忽略了问题的“经过一定时间”的部分。

您还可以通过调用 pidof java | 来完成此操作。 awk '{print "sleep 10; Kill "$1}' | bash 但选择权在你。我一般用ps aux。

ps aux | awk '/java/ {print "sleep 10; kill "$2}' | bash

in Ubuntu, ps -aux throws an syntax error, where ps aux works.

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.

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.

凑诗 2024-12-16 12:28:22

您可能必须使用 Runtime.exec 并抓取它们的输出。我认为 Java 程序无法轻松检查/访问它未创建的进程。

You'll probably have to invoke the ps and kill Unix commands using Runtime.exec and scrape their output. I don't think a Java program can easily inspect / access processes it didn't create.

許願樹丅啲祈禱 2024-12-16 12:28:22

查找Linux下java进程的pid

可以参考这个链接...它告诉你如何找到特定类名的pid

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文