内部带有 nohup 的脚本无法正确退出

Scripts with nohup inside don't exit correctly

我们有脚本可以使用 nohup 在后台进行一些处理并触发作业。当我们从 Oracle OEM(或者它可以是任何调度程序作业)调度此脚本时,我看到以下错误并将状态显示为失败但脚本实际上已完成且没有问题。 nohup启动backup ground job时如何正确退出脚本?

Remote operation finished but process did not close its stdout/stderr

文件:test.sh

#!/bin/bash
# do some processing
...
nohup ./start.sh 2000 &

# end of the script

通过以这种方式执行 start.sh,您允许它声明对 test.sh 的输出文件描述符 (stdout/stderr) 的部分所有权。因此,当 most bash 脚本退出时,它们的文件描述符为它们关闭(由操作系统),test.sh 的文件描述符无法关闭,因为 start.sh 仍然对它们拥有所有权。

解决方案是不要让 start.sh 声明与 test.sh 正在使用的相同的输出文件描述符。如果你不关心它的输出,你可以这样启动它:

nohup ./start.sh 2000 1>/dev/null 2>/dev/null &

告诉新进程将其标准输出和标准错误发送到 /dev/null。如果您确实关心它的输出,那么只需将其捕获到更有意义的地方即可:

nohup ./start.sh 2000 1>/path/to/stdout.txt 2>/path/to/stderr.txt &