从 java 内部启动一个 bash 脚本,该脚本在 jvm 退出后仍然存在
我正在尝试从 java 内部运行一个 bash 脚本,该脚本将在 JVM 退出后继续存在。我当前的尝试看起来像这样:
String[] linCmd = {"/bin/bash", "-c", "\"set +m; shopt -u huponexit; nohup "
+ "myScript.sh 2>&1 > /dev/null &\""};
pb = new ProcessBuilder(linCmd);
//Other stuff to monitor and start pb
但我没有看到 myScript.sh 实际上启动(在 top 或 ps 中)。顺便说一句,单独的 bash shell 的原因是因为我需要 set +m
并且不想用它来破坏原始文件。另外,nohup 和 shopt -u huponexit 可能是多余的,但我已经尝试过单独使用它们,但似乎无法使其正常工作。
有什么想法吗?
I'm trying to run a bash script from inside java that will live on after the JVM exits. My current attempt looks something like this:
String[] linCmd = {"/bin/bash", "-c", "\"set +m; shopt -u huponexit; nohup "
+ "myScript.sh 2>&1 > /dev/null &\""};
pb = new ProcessBuilder(linCmd);
//Other stuff to monitor and start pb
But I'm not seeing myScript.sh actually start up (in top or ps). By the way, the reason for the separate bash shell is because I need the set +m
and don't want to corrupt the original with that. Also the nohup
and shopt -u huponexit
may be redundant, but I've tried it without with each alone and can't seem to get it working right.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试一下,不要在“set +m ... &”两边添加额外的引号通过将整个内容作为该数组的第三个元素,您可以告诉 java 这整个内容是第三个参数。引号应该是不需要的,我认为它们可能会干扰。
Try it without adding the extra quotation marks around your "set +m ... &" By including the entire thing as the third element of that array, you're telling java that this whole thing is the third argument. The quotes should be unneeded, and I think they might interfere.
您能否将进程置于后台,添加对
disown
< 的调用/a> 那个后台进程然后让 shell 退出?Can you just background the process, add a call to
disown
that backgrounded process and then let the shell exit?