Tee does not exit after pipeline it's on has finished [closed]
Asked Answered
H

0

11

So, I've got a rather long and involved script intended to be used by people who aren't going to want to dig into anything that goes wrong.

Recently, during testing, the script froze inexplicably. Long story short, I executed a command in a subshell in order to be able to tee stdout and stderr to my log file:

(/path/to/script -i -ran 2>&1; ) | tee -a /path/to/mylogfile

The script was no longer in the process tree, no longer running, and seems to have exited completely because the file that it writes as its last action was there and not open. However, tee remained, obstinately. I killed tee, and the script proceeded merrily on its way. This is the first time this has happend, and I want to know if there's anything I can do to prevent it from happening again. Any ideas would be most appreciated.

Hothouse answered 1/3, 2012 at 20:24 Comment(9)
First, a side comment, the ";" isn't needed. Second, can you reproduce this problem? I suspect that something is not quite as you believe here, because tee will indeed exit when the process writing to the pipe exits. If you can reproduce the problem we can debug it.Ridgway
Yes, and to debug deeper, use this shebang : #!/bin/bash -x to figure out what's happensNativeborn
The only time I have experienced this was when "/path/to/mylogfile" was a named pipe (usually generated with mkfifo or mknod -p) If it is a named pipe, tee will sit there until your logger processed it. Try file /path/to/mylogfile to see (its pretty common to do this for logging so that a daemon can process any errors etc...)Chelicera
I'll try check the file (although it should be a file, as tee is the only thing that is both creating and appending to it.) Also, I will check lsof to see if the pipe feeding in to tee is still open, and see if anything else has open either that pipe or the file tee is writing to.Hothouse
Well, I don't have lsof on the system (crap), but when I attempted to run /usr/bin/proc/pfiles <process number> of the tee, it seems to have shaken the tee loose, because it closed it out and made it move on. Which is less than helpful, because that's harder to automate. Also, technosaurus, it is just a txt ascii file which I did verify. I wasn't able to find the pipe because, like I said, the process exited as soon as I ran pfiles...Hothouse
When it happens again check with "ps fax" which is the father of the tee process and if it has a sibling and its state (zombie, sleeping, unint sleep, stopped, running). It's very strange because when tee receive EOF on stdin it should exit.Amperehour
You seem to have a contradiction in your statement: "The script was no longer in the process tree, ... I killed tee and the script proceeded merrily on its way." If you killed tee and noticed that the script proceeded, then you must have missed seeing the script in the process tree while tee was still running/waiting for input as expected. If the problem repeats itself, make sure the partition that your script and tee are writing to isn't running out of space during execution. If that's not the case, you'll want to run your script in debug mode as mentioned by sputnick.Longawa
@Saxon: I fear that the question is ambiguous rather than contradictory. I think there is a big (long, involved) script — call it uberscript — that contains in part the invocation of /path/to/script and tee. The script that was not in the process tree was /path/to/script; when the orphaned tee was killed, it was the uberscript that went merrily on its way.Schellens
welp, 12 years later, and this hit me too. I think set -o pipefail might be needed to tell tee to disconnect when the process being piped in exits. In my case, the process I'm piping into tee uses #!/bin/sh for shebang, so I'm wondering if that's also part of the problem... Anyway, going to keep digging. Will post an answer if I find anything conclusive.Webbed

© 2022 - 2024 — McMap. All rights reserved.