Runtime.getRunTime()。exec的行为不像C语言" system()"命令

问题描述:

在C中,我可以在启动过程退出后在后台运行一个长时间的阻止过程(并且还有继续运行)。

In "C", I can run a long blocking process in the background (AND HAVE IT CONTINUE TO RUN) after the starting process has exited.

void main(void)
{
      system("some_long_blocking_process &");
      exit();
}

// "some_long_blocking_process" is still running here (DESIRED BEHAVIOR)

Java的getRuntime()。exec()没有这种行为。相反,some_long_blocking_process在Java进程结束时立即结束。

Java's getRuntime().exec() DOESN'T have this behavior. Instead, "some_long_blocking_process" ends immediately when the Java process ends.

任何人都知道我如何在Java中重新获得这种行为?

Anyone know how I can recapture this behavior in Java?


  1. 我是使用Java 1.4(无流程构建器)

  2. 我特别希望启动长阻塞过程并立即退出(没有waitFor()等等)

  3. 我已经尝试过的事情(流程运行正常,但我仍然会遇到相同的不良行为)

    • 添加nohup和在前台运行(nohup some_long_process)

    • 添加nohup并在后台运行(nohup some_long_process&)

    • 在前台运行(some_long_process)

    • 在后台运行(some_long_process&)

  1. I am using Java 1.4 (No process builder)
  2. I specifically am looking to start the long blocking process and to exit immediately (no "waitFor(), etc.)
  3. Things I have already tried (the process runs correctly, but I still get the same undesired behavior)
    • adding "nohup" and run in foreground ("nohup some_long_process")
    • adding "nohup" and running in background ("nohup some_long_process &")
    • run in foreground ("some_long_process")
    • run in background ("some_long_process &")

感谢!

感谢所有建议......我决定使用jtahlborn的回答(它对我有用)

Thanks to all the suggestions... I've decided to use jtahlborn's answer (it worked for me)

我们用java实现这一目标的唯一方法是添加另一个r层脚本。你需要一个简单的包装脚本来调用你真正想要运行的应用程序,例如:

the only way we were able to achieve this with java was to add another layer of script. you need a simple wrapper script which invokes the app you actually want to run, e.g.:

runner.sh:

runner.sh:

#!/bin/sh

nohup "$@" > /dev/null 2>&1 &

然后从你的java程序中调用/ bin / sh runner.sh the real command。

then invoke "/bin/sh runner.sh the real command" from your java program.