e2e8
e2e8

Reputation: 119

Reading from named pipe from background process

I have a program that has a window and also outputs to stdout. I am reading the programs output and writing one line of that output to a pipe. This is done in the background while the program is still running. I send a command to the window and wait for my single line from grep. However, even though the program has already produced this text tail will not exit unitl I stop the program.

I want tail to return this one line as soon as it can so I can terminate the program with "\e" to the window.

bin/Prog | grep "TEXT" > /tmp/pipe2 &
xvkbd -window Window -text "2"
tail -n1 /tmp/pipe2 >> out.t
xvkbd -window Window -text "\e"

Upvotes: 0

Views: 1106

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 753695

The tail command doesn't know it has reached the last line of input until it gets EOF, and it won't get EOF until the grep terminates and closes its standard output. Also, grep will buffer its output when the output device is not 'interactive', and a named pipe is not 'interactive', so grep's output won't be written to the pipe until its input generates EOF, which won't happen until the bin/Prog exits. So, until the program exits, the grep and the tail are stuck, and since you are waiting for grep and tail to exit before telling the program to exit, you have a deadlock.

You might do better with tail -n +1 which looks for one line of output at the start (or sed 1q or head -n 1 or …). However, you're still stuck with grep buffering its output, which leaves you in a quandary.

Upvotes: 2

Related Questions