Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I've got a ksh script that calls:

while read LINE do...

The input has been redirected (not shown) so that it reads input from a pipe.

The problem is that when the writer to the pipe closes, read returns false and exits the loop.

I want it to keep reading after the pipe is closed (the writing process may re-start from time to time, causing this situation).

I tried wrapping the whole read LINE within a subshell and looping over the whole subshell, but read just get an empty line after the pipe is closed rather than blocking until there is more input as it does when the script starts normally.

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

A pipe is created in the kernel as a communication method between two processes. It's usually a parent/child relationship. If a process exits, there is no pipe. There is no way to attach another process to the other end of the pipe in the kernel. The kernel automatically starts a shutdown on the pipe, and gives EPIPE to any readers.

What you can do though is create a named pipe. It exists in the file system, and is not closed when you exit any writer program. You can even even have multiple writers to the pipe, though I'd not suggest this - the 'chunks' you think are writing are written in different chunks because of buffering.

You create the pipe in a known directory with mkfifo /path/to/pipe and you redirect stdin or stdout from/to it depending if you're a reader or writer.

share|improve this answer
add comment

Once a pipe is closed, it cannot be re-opened. I don't know all the specifics about named pipes on the file system, but if you're just doing something like cat somefile.txt | myprogram then as soon as cat closes the pipe, the pipe is gone.

share|improve this answer
    
> isn't a pipe. It's an output redirection specifier. –  amphetamachine Jun 10 '11 at 19:51
    
@amphetamachine: Whooops. Thanks for catching that –  Darth Android Jun 10 '11 at 19:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.