Checking if the other end of a fifo is open

Dave Smith dave at thesmithfam.org
Thu Apr 9 14:56:18 MDT 2009


Nicholas Leippe wrote:
> kill -USR1 # should do it
>
> $$ # has the pid of the current process--just pass that in to the child
>   

Okay, got it. This sandbox script simulates a blocking call (sleeping 
for 2 seconds in this case) in a backgrounded subshell, which signals 
when it's done. Thanks Nicholas!

#!/bin/bash

trap "flag=1" SIGUSR1

flag=0
(sleep 2 ; kill -SIGUSR1 $$) &

# Wait for the flag to get raised, or give up after a while:
count=0
while true; do
     if [ $flag -eq 1 ]; then
         echo Yay, it worked.
         break
     else
         echo Still waiting
         let count=$count+1

         if [ $count -gt 100 ]; then
             echo Giving up
             exit 1
         fi
     fi
     sleep 0.1
done




More information about the PLUG mailing list