从 java 内部启动一个 bash 脚本,该脚本在 jvm 退出后仍然存在

发布于 2024-11-25 11:41:39 字数 487 浏览 3 评论 0原文

我正在尝试从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情深如许 2024-12-02 11:41:39

尝试一下,不要在“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.

无人问我粥可暖 2024-12-02 11:41:39

您能否将进程置于后台,添加对 disown< 的调用/a> 那个后台进程然后让 shell 退出?

否认[-ar] [-h] [工作规范...]

如果没有选项,每个作业规范都会从活动作业表中删除。如果给出 -h 选项,则每个作业规范不会从表中删除,但会被标记,以便在 shell 收到 SIGHUP 时不会将 SIGHUP 发送到作业。如果不存在作业规范,并且未提供 -a 和 -r 选项,则使用当前作业。如果未提供 jobspec,则 -a 选项表示删除或标记所有作业;不带 jobspec 参数的 -r 选项将操作限制为正在运行的作业。除非作业规范未指定有效作业,否则返回值为 0。

Can you just background the process, add a call to disown that backgrounded process and then let the shell exit?

disown [-ar] [-h] [jobspec ...]

Without options, each jobspec is removed from the table of active jobs. If the -h option is given, each jobspec is not removed from the table, but is marked so that SIGHUP is not sent to the job if the shell receives a SIGHUP. If no jobspec is present, and neither the -a nor the -r option is supplied, the current job is used. If no jobspec is supplied, the -a option means to remove or mark all jobs; the -r option without a jobspec argument restricts operation to running jobs. The return value is 0 unless a jobspec does not specify a valid job.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文