Killing a process using Java

前端 未结 8 1847
名媛妹妹
名媛妹妹 2020-11-22 11:08

I would like to know how to \"kill\" a process that has started up. I am aware of the Process API, but I am not sure, If I can use that to \"kill\" an already running proces

8条回答
  •  梦谈多话
    2020-11-22 11:52

    It might be a java interpreter defect, but java on HPUX does not do a kill -9, but only a kill -TERM.

    I did a small test testDestroy.java:

    ProcessBuilder pb = new ProcessBuilder(args);
    Process process = pb.start();
    Thread.sleep(1000);
    process.destroy();
    process.waitFor();
    

    And the invocation:

    $ tusc -f -p -s signal,kill -e /opt/java1.5/bin/java testDestroy sh -c 'trap "echo TERM" TERM; sleep 10'
    

    dies after 10s (not killed after 1s as expected) and shows:

    ...
    [19999]   Received signal 15, SIGTERM, in waitpid(), [caught], no siginfo
    [19998] kill(19999, SIGTERM) ............................................................................. = 0
    ...
    

    Doing the same on windows seems to kill the process fine even if signal is handled (but that might be due to windows not using signals to destroy).

    Actually i found Java - Process.destroy() source code for Linux related thread and openjava implementation seems to use -TERM as well, which seems very wrong.

提交回复
热议问题