如何杀死runtime.exec? [英] how to kill runtime.exec?

查看:162
本文介绍了如何杀死runtime.exec?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的jframe中使用下面的代码,以便通过java程序执行shell脚本,但是当我实现其他方法来停止运行过程时,这个方法被第一个阻止,我无法阻止我的程序。所以我要杀死新进程,问题是如何获取进程的pid以及如何将其置于后台,以便杀死它。

I use the code below , in my jframe in order to execute a shell script via java program, but when i implement an other method to stop runing process, this method is blocked by th first and i can't stop my program. So i msut kill the new process, the problem is how to get the pid of the process and how to put it in background, in order to kill it.

public static void execShellCmd(String cmd) {
    try {

        testPath = getPath();

        System.out.println(cmd);
        Runtime runtime = Runtime.getRuntime();
        process = runtime.exec(new String[] { "sh",
                testPath + "/install.sh", cmd, "&" });
        int exitValue = process.waitFor();
        value = exitValue;
        System.out.println("exit value: " + exitValue);
        BufferedReader buf = new BufferedReader(new InputStreamReader(
                process.getInputStream()));
        String line = "";
        BufferedWriter bufferedWriter = null;
        while ((line = buf.readLine()) != null) {
            System.out.println("exec response: " + line);
            log = line;
            writeToFile(line);
        }
    } catch (Exception e) {
        System.out.println(e);
    }
}


推荐答案

如果您的其他方法可以访问原始 exec 返回的 Process 对象,然后您只需调用 process.destroy()来杀死正在运行的进程。您不需要知道本机PID。这是一个或多或少的完整示例,使用 SwingWorker (我可能忘记了一些try / catch块):

If your "other method" has access to the Process object returned by the original exec then you can simply call process.destroy() to kill the running process. You don't need to know the native PID. Here's a more-or-less complete example using SwingWorker (there may be a few try/catch blocks I've forgotten):

private Process runningProcess = null;

public static void execShellCmd(String cmd) {
    if(runningProcess != null) {
        // print some suitable warning about process already running
        return;
    }
    try {

        testPath = getPath();

        System.out.println(cmd);
        Runtime runtime = Runtime.getRuntime();
        runningProcess = runtime.exec(new String[] { "sh",
                testPath + "/install.sh", cmd });

        new SwingWorker<Integer, Void>() {
            protected Integer doInBackground() {
                // read process output first
                BufferedReader buf = new BufferedReader(new InputStreamReader(
                    process.getInputStream()));
                String line = "";
                while ((line = buf.readLine()) != null) {
                    System.out.println("exec response: " + line);
                    log = line;
                    writeToFile(line);
                }

                // only once we've run out of output can we call waitFor
                while(true) {
                    try {
                        return runningProcess.waitFor();
                    } catch(InterruptedException e) {
                        // do nothing, wait again
                    } finally {
                        Thread.interrupted();
                    }
                }
                return -1;
            }

            protected void done() {
                runningProcess = null;
            }
        }.execute();
    }
}

public static void stopProcess() {
    if(runningProcess != null) {
        runningProcess.destroy();
    }
}

只要 execShellCmd stopProcess 仅从事件调度线程调用,然后不需要同步,因为对 runningProcess的所有访问字段发生在同一个线程上( SwingWorker 的点是 doInBackground 在工作线程上发生但是已完成在EDT上运行。)

As long as execShellCmd and stopProcess are only called from the event dispatch thread then no synchronization is necessary, as all accesses to the runningProcess field happen on the same thread (the point of SwingWorker is that doInBackground happens on a worker thread but done is run on the EDT).

这篇关于如何杀死runtime.exec?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆