Why does tee wait for all subshells to finish?

流过昼夜 提交于 2019-12-10 21:20:23

问题


I have a server script that runs mysqld and forks to continue running. As an example:

./mysqld <parameters> &
echo "Parent runs next line in script."
<do more stuff>

Why does tee wait for the child process to end before it ends itself?

EDIT:

For example, the following always hangs:

./myscript | tee -a logfile.log

回答1:


Because it can't be sure it has tee'd all the output if the child process is still running (and still has its standard output open).

Since the parent and child use the same standard output (which is connected to tee's input, due to the pipe), there is no way for tee to distinguish them. Since it consumes all input, both the parent and child must close their standard output (or terminate) before tee will see and end-of-input condition.

If you want tee to exit when the parent script does, you should redirect output of the child (to a file or to /dev/null for example).



来源:https://stackoverflow.com/questions/18595199/why-does-tee-wait-for-all-subshells-to-finish

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!