简体   繁体   English

程序终止时,用`exec`终止进程运行

[英]Terminate process run with `exec` when program terminates

I have a java program that runs another (Python) program as a process. 我有一个java程序,它运行另一个(Python)程序作为进程。

Process p = Runtime.getRuntime().exec("program.py", envp);

If the java program finish processing, the Python process is finished as well. 如果java程序完成处理,Python进程也会完成。 The finish command sends a signal to the Python process to close it. finish命令向Python进程发送一个信号以关闭它。

In normal situation the process is closed this way: 在正常情况下,该过程以这种方式关闭:

BufferedWriter output = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
output.write("@EOF\n");
output.flush();

However, when the java program crashes, the process is not closed. 但是,当java程序崩溃时,进程不会关闭。 The closing command is not send due to the crash. 由于崩溃,不发送关闭命令。 Is it possible to terminate the process automatically every time the program is terminated? 是否可以在程序终止时自动终止进程?

hey @czuk would a ShutdownHook hook be any use? 嘿@czuk会对ShutdownHook挂钩有什么用吗? This will deal with the following scenarios 这将处理以下方案

The Java virtual machine shuts down in response to two kinds of events: Java虚拟机关闭以响应两种事件:

  • The program exits normally, when the last non-daemon thread exits or when the exit (equivalently, System.exit) method is invoked, or 当最后一个非守护程序线程退出或调用退出(等效,System.exit)方法时,程序正常退出,或者

  • The virtual machine is terminated in response to a user interrupt, such as typing ^C, or a system-wide event, such as user logoff or system shutdown. 响应于用户中断(例如,键入^ C)或系统范围的事件(例如用户注销或系统关闭),终止虚拟机。

When the system unexpectedly crashes this is not so easy to capture. 当系统意外崩溃时,这并不容易捕获。

Perhaps use the Thread.setDefaultUncaughtExceptionHandler method? 也许使用Thread.setDefaultUncaughtExceptionHandler方法?

Assuming that by crashing you mean that the Java program throws an exception, I would just kill the Python process when that happens. 假设崩溃意味着Java程序抛出异常,我会在发生这种情况时终止Python进程。 I haven't seen your code but: 我还没有看到你的代码,但是:

class Foo {

    Process p;

    private void doStuff() throws Exception {
        p = Runtime.getRuntime().exec("program.py", envp);
        // More code ...
    }

    private void startStuff() {
        try {
            doStuff();
        } catch (Exception e) {
            p.destroy();
        }
    }

    public static void main(String[] args) {
        Foo foo = new Foo();
        foo.startStuff();
    }
}

Something like this should work if only exceptions that cause the program to crash escapes from doStuff() . 如果只有导致程序崩溃的异常从doStuff()逃脱,那么这样的东西应该有效。

Even if you don't expect the program to crash in this way when it is finished I think this approach is better than perhaps wrapping it in a shell script that in some way kill the Python process. 即使您不希望程序在完成时以这种方式崩溃,我认为这种方法比将它包装在shell脚本中更好,因为shell脚本在某种程度上会破坏Python进程。 Handling it in your program is less complex and it might even be a good idea to keep the code once the program is finished, since you might still have bugs that you don't know about. 在程序中处理它不那么复杂,一旦程序完成后保留代码甚至可能是个好主意,因为你可能仍然有你不知道的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM