Named pipe hanging?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Named pipe hanging?
# 15  
Old 03-19-2014
Quote:
Originally Posted by Don Cragun
So, try:
Code:
#!/bin/ksh
touch test.file
LOG=./tmp.log
(
        echo "Hello world"
        ls -ltr test.file
        echo "Goodbye world"
) 2>&1 | tee ${LOG}

Ok, so now at the end, check/show the return code of the ls command ?

Code:
#!/bin/ksh
touch test.file
LOG=./tmp.log
(
        echo "Hello world"
        ls -ltr test.file
        echo "Goodbye world"
) 2>&1 | tee ${LOG}

echo "Return code is: ???"

I've tried just capturing it inside that block, but accessing it outside the brackets fails (null value).
# 16  
Old 03-19-2014
You said you wanted to capture everything in the script in the log file. So you've changed the rules again... grumble, grumble, grumble...
Code:
#!/bin/ksh
touch test.file
AFTERLOG=./$$.after.log
LOG=./tmp.log
(
        echo "Hello world"
        echo "1st echo return code is: $?" > "$AFTERLOG"
        ls -ltr test.file
        echo "Return code is: $?" >> "$AFTERLOG"
        echo "Goodbye world"
        echo "2nd echo return code is: $?" >> "$AFTERLOG"
) 2>&1 | tee "${LOG}"
cat "$AFTERLOG"
rm -f "$AFTERLOG"

# 17  
Old 03-19-2014
Quote:
Originally Posted by Ditto
Changed my code to this (added the exec), and now it works Smilie
This is what has been happening in your original code ...

When tee opens the fifo for reading, if no other process has yet written to the fifo, tee will block (hang/sleep) until another process opens the fifo and writes to it.

Each echo statement will attempt to open the fifo, write to it, and then close it. Most likely, when this succeeds, it is because both echoes have written to the fifo before tee has a chance to read it.

Adding ls greatly increases the chances of tee reading between the echoes. If that happens, after reading the first echo's text, tee will detect EOF and exit. When the second echo opens the fifo for writing, it will hang until something opens the fifo for reading (which will never happen with your sample script).

exec 5>fifo prevents the hang by ensuring that there is always a write file descriptor for the fifo. That descriptor is never used, but since it has permission to write, so long as it exists, tee will not read EOF; instead, tee will block until someone writes (in this case, the second echo).

Regards,
Alister
These 3 Users Gave Thanks to alister For This Post:
# 18  
Old 03-19-2014
Quote:
Originally Posted by Don Cragun
You said you wanted to capture everything in the script in the log file. So you've changed the rules again... grumble, grumble, grumble...
apologies, I only changed the rules once Smilie I editted my original post after your first post, and clarified the two things I'm trying to do.

Quote:
Code:
#!/bin/ksh
touch test.file
AFTERLOG=./$$.after.log
LOG=./tmp.log
(
        echo "Hello world"
        echo "1st echo return code is: $?" > "$AFTERLOG"
        ls -ltr test.file
        echo "Return code is: $?" >> "$AFTERLOG"
        echo "Goodbye world"
        echo "2nd echo return code is: $?" >> "$AFTERLOG"
) 2>&1 | tee "${LOG}"
cat "$AFTERLOG"
rm -f "$AFTERLOG"

hmm, that "sort of" works, but then I have to go digging the error code out of the script and stuff it into a variable if I need to use it / check it.

Thanks ... I think what I found with the exec 5>&PIPE seems to work more consistently.
And it seems from the responses, that another (cleaner) option probably doesn't exist.

Don't want to take any more of your time Smilie Sorry for confusion, thanks for help. I think I have something working, and I think I understand what's going on.

Smilie Cheers!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to test named pipe file?

Hi ALL, How can I test a given file name exists and if it is a named pipe file in shell script ? Thanks............ (2 Replies)
Discussion started by: mycode.in
2 Replies

2. Programming

Named pipe behavior in Linux

Hi All ! I try to collect importent events from syslog and in my syslog conf, there is something like this: *.* |/logs/ipes/SLpipe1 I have a program, which opens this pipe and reads the messages from it. But how this pipe works ? Where can I probably read something about the details,... (3 Replies)
Discussion started by: mabra
3 Replies

3. Shell Programming and Scripting

Named pipe performance

Hi, I am getting data into a Named pipe. Does Named pipe have any size restriction; I know it does not have any storage and it just passes on the data to the next process. I want to know, if there will be a difference in the Named pipe performance if the data input is more. (I am using DB2... (1 Reply)
Discussion started by: sudvishw
1 Replies

4. Shell Programming and Scripting

Using Named pipe in shell script

Hi, I want to use a Named pipe to get input from a growing file for further processing. When I prototype this scenario using a while loop, the data is not written to the named pipe. This the script I use to get data into the Named pipe: #!/bin/ksh mkfifo pipe while (( n <= 10 )) do echo... (2 Replies)
Discussion started by: sudvishw
2 Replies

5. Shell Programming and Scripting

pipe to file named with date

I would like to pipe (redirect ? - what is the right term?) the output of my script to a file named with the current date. If I run this at a command prompt: date +'%Y%m%d" ...it returns "20110429" OK, that's good... so I try: ./script.sh > "'date +%Y%m%d'.csv" I get a file... (1 Reply)
Discussion started by: landog
1 Replies

6. UNIX for Dummies Questions & Answers

Filtering mail into a named pipe

Hello, On my machine, all mail is stored in my /var/spool/mail. IS there a way to direct all mail that goes there into a namep pipe? Thank you, Dado (4 Replies)
Discussion started by: dadoprso
4 Replies

7. UNIX for Dummies Questions & Answers

Named Pipe contents to a file

I want to copy the contents of a named pipe to a file. I have tried using: cat pipe.p >> transcript.log but I have been unsuccessful, any ideas? (4 Replies)
Discussion started by: carl_vieyra
4 Replies

8. UNIX for Dummies Questions & Answers

Named PIPE

Gurus, I've a File Transaction Server, which communicates with other servers and performs some processing.It uses many Named PIPE's. By mistake i copied a named PIPE into a text file. I heard that PIPE files shouldn't be copied.Isn't it? Since it's a production box, i'm afraid on... (2 Replies)
Discussion started by: Tamil
2 Replies

9. Programming

IPC using named pipe

Hi All, I am facing a vague issue while trying to make two process talk to each other using named pipe. read process ========= The process which reads, basically creates FIFO using mkfifo - ret_val = mkfifo(HALF_DUPLEX, 0666) func. It then opens the pipe using open func - fd = open... (2 Replies)
Discussion started by: sharanbr
2 Replies

10. UNIX for Advanced & Expert Users

IPC using named pipe

Hi All, I am facing a vague issue while trying to make two process talk to each other using named pipe. read process ========= The process which reads, basically creates FIFO using mkfifo - ret_val = mkfifo(HALF_DUPLEX, 0666);) func. It then opens the pipe using open func - fd =... (1 Reply)
Discussion started by: sharanbr
1 Replies
Login or Register to Ask a Question