3

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
Community
  • 1
  • 1
neverendingqs
  • 4,006
  • 3
  • 29
  • 57

1 Answers1

5

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

davmac
  • 20,150
  • 1
  • 40
  • 68
  • And the pipeline waits until both the parent and child has ended? – neverendingqs Sep 03 '13 at 15:13
  • tee waits until its input file descriptor returns no more data (read operation fails or returns EOF). That won't happen until both the parent and child close their stdout (or terminate). – davmac Sep 03 '13 at 16:04
  • you can avoid this, by the way, by redirecting the output of the child process. – davmac Sep 04 '13 at 15:04
  • sounds great @davmac - we redirected the output of the child process, but I wanted to confirm that we understood the problem when I asked the question. Thanks for your help! – neverendingqs Sep 04 '13 at 18:42