Why does tee wait for all subshells to finish?

2019-07-04 14:20发布

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条回答
看我几分像从前
2楼-- · 2019-07-04 14:50

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

查看更多
登录 后发表回答