用Java创建Nohup进程 [英] Creating a Nohup Process in Java

查看:572
本文介绍了用Java创建Nohup进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ProcessBuilder,我一直在尝试创建一个独立的进程,该进程在JVM终止时不会终止,但是似乎没有任何作用.

Using ProcessBuilder, I've been trying to create an independent process that doesn't get terminated when the JVM gets terminated, but nothing seems to work.

我已经尝试过/usr/bin/nohup commands,但是当启动它的JVM终止时,它似乎仍然终止.用Java有什么方法可以做到这一点?

I've tried /usr/bin/nohup commands, but that still seems to terminate when the JVM that launched it is terminated. Is there any way to accomplish this in Java?

推荐答案

首先,首先让我们编写一个测试脚本来验证您所看到的内容:

Well, first things first lets write a test script that validates what you're seeing:

$ cat /tmp/test.sh 
#!/bin/bash

for sig in SIGINT SIGTERM SIGHUP; do
  trap "echo Caught $sig" $sig
done

echo Traps registered, sleeping
sleep 10
echo Done sleeping, exiting

当我通过以下方式调用它时:

When I invoke this via:

new ProcessBuilder("/tmp/test.sh").inheritIO().start().waitFor(1, TimeUnit.SECONDS);

我看到Java进程在1秒钟后终止(因为waitFor()超时),但是子进程继续运行(在JVM退出后,Done sleeping消息被打印到控制台).这与此问题中讨论的内容一致.

I see the Java process terminate after 1 second (since waitFor() timed out), but the subprocess keeps going (the Done sleeping message is printed to the console after the JVM exits). This lines up with what's discussed in this question.

因此,我建议做的第一件事是验证您最初的假设,即该子进程实际上已被杀死;也许是其他原因导致它死亡的其他地方出了问题.如果子进程正在生成您看不到的错误消息,使用.inheritIO()可以帮助调试.

So the first thing I'd suggest doing is validating your initial assumption that the subprocess is in fact being killed; perhaps something else is going wrong that causes it to die for another reason. Using .inheritIO() can help debugging, in case the subprocess is generating error messages you're not seeing.

所有这些,nohup可能不是您想要的,正如那个家伙"所指出的那样; nohup仅导致进程忽略SIGHUP,并且仍然会尊重其他信号,尤其是SIGINTSIGTERM(Process.destroy() 此问题.

All that said, nohup may not be what you want, as 'that other guy' notes; nohup only causes a process to ignore SIGHUP, and it will still respect other signals, notably SIGINT and SIGTERM (Process.destroy() sends a SIGTERM, for instance). See this question for more.

与编程中的所有问题一样,引入更多的层可能会有所帮助:)

As with all problems in programming, introducing more layers would probably help :)

创建一个处理所需的弃用逻辑的shell脚本,例如

Create a shell script that handles the desired disowning logic, e.g.

$ cat start-and-disconnect.sh
#!/bin/bash

# obviously tweak this as necessary to get the behavior you want,
# such as redirecting output or using disown instead of nohup
nohup "$@" & 

此处的优点是Bash(或您喜欢的任何Shell)比Java具有更好的过程管理控制,现在您可以单独对其进行测试,而无需编译和运行Java应用程序.

The advantage here is that Bash (or whatever shell you prefer) has better process-management control than Java, and now you can test it in isolation, without needing to compile and run your Java application.

一旦您对脚本满意,您的Java代码就会变成:

Once you're happy with the script, your Java code simply becomes:

new ProcessBuilder("/path/to/start-and-disconnect.sh", "/path/to/your_binary.sh")
    .inheritIO().start().waitFor();

您可以放心地.waitFor()调用完成,因为start-and-disconnect.sh将在开始其子过程后退出.

You can safely .waitFor() the call to complete, since start-and-disconnect.sh will exit after starting its subprocess.

这篇关于用Java创建Nohup进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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