java nohup,用Java创建Nohup进程

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.

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);

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.

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.

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 :)

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 "$@" &

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.

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();

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 如果您想重启运行在Linux上的Java进程,可以使用以下步骤: 1. 查找正在运行的Java进程进程ID(PID)。您可以通过运行以下命令来查找正在运行的Java进程: ``` ps -ef | grep java ``` 这将列出所有正在运行的Java进程,并显示它们的PID。 2. 使用kill命令停止正在运行的Java进程。您可以使用以下命令停止进程: ``` kill -9 <PID> ``` 其中<PID>是步骤1中找到的Java进程的PID。 3. 使用nohup命令启动Java进程。您可以使用以下命令启动Java进程: ``` nohup java -jar <your_jar_file_name>.jar & ``` 其中<your_jar_file_name>.jar是您的Java应用程序的JAR文件名。 注意,使用nohup命令启动Java进程将使Java进程在后台运行,并且在您注销或关闭终端会话时不会停止。 ### 回答2: 在Linux系统中,使用nohup命令可以在终端关闭后保持进程继续运行,即使当前用户已退出。重启Java进程也可以使用nohup来实现。 首先,在终端中使用以下命令来启动Java进程,并且使用nohup命令使其在后台运行: ``` nohup java -jar your_java_program.jar & ``` 这条命令中的`your_java_program.jar`是你要运行的Java程序包的文件名。这个命令将会在终端关闭后继续在后台运行Java进程。 如果需要重启Java进程,我们可以通过以下步骤来实现: 1. 首先,需要找到Java进程进程ID(PID)。可以使用ps命令来查看所有正在运行的Java进程,并找到对应的PID: ``` ps aux | grep java ``` 这个命令将会列出所有包含"java"关键字的进程信息,找到你要重启的Java进程对应的PID。 2. 找到PID后,可以使用kill命令来终止该进程: ``` kill PID ``` 将PID替换为上一步骤中找到的Java进程的PID。 3. 终止进程后,再次使用nohup命令来重新启动Java进程: ``` nohup java -jar your_java_program.jar & ``` 这个命令会启动一个新的Java进程,并将其放入后台运行。 通过以上步骤,就可以实现在Linux系统中使用nohup命令重启Java进程。无论是在终端关闭后还是手动重启,都可以保持Java进程继续运行。 ### 回答3: 在Linux上,我们可以使用nohup命令重启Java进程nohup命令的作用是在当前终端断开连接后仍然能够继续执行命令,并且将命令的输出重定向到一个指定的文件中。 要重启一个Java进程,首先需要找到该进程进程ID(PID)。我们可以使用如下命令找到正在运行的Java进程: ``` ps -ef | grep java ``` 查找到相关的进程后,可以根据需要选择要重启的进程。然后,使用kill命令终止该进程: ``` kill -9 <PID> ``` 需要注意的是,上述命令中的`<PID>`应替换为要终止的Java进程的实际进程ID。 接下来,使用nohup命令以后台方式启动Java进程,并将输出重定向到一个日志文件中: ``` nohup java -jar your_jar_file.jar > your_log_file.log & ``` 在上述命令中,`your_jar_file.jar`应替换为你实际的Java可执行文件,而`your_log_file.log`则是你希望将输出信息存储的日志文件名。 最后加上`&`符号,使得该命令在后台运行。 通过上述命令,我们成功地使用nohup命令重启了Java进程,并且在断开连接后仍然能够继续运行。同时,重定向的日志文件也可以用于后续的排错和日志记录。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值