Skip to main content
added 83 characters in body
Source Link
Alexander Mills
  • 10.9k
  • 27
  • 120
  • 214

Right now, I have this crappy check to see if a named pipe is being read from:

is_named_pipe_being_read() {

    local named_pipe="$1" 
    echo "unlocked" > "$named_pipe" & 
    pid="$!"
    # Wait a short amount of time
    sleep 0.25
    # Kill the background process. If kill succeeds, then
    # the write was blocked 
    ( kill -PIPE "$pid" ) &> /dev/null
}

if the kill works (exits with 0) then it means that nobody was reading from the pipe.

But instead of having a 0.25 second delay and starting up an unnecessary process, I am looking for a way to check the named pipe to see if it's opened for reading? Is there some way to determine if something is reading from it?

Note: I can't read from the pipe in this call, I can only write to it (because with named pipes the order at which readers are attached does not seem to be respected - or perhaps it's the most recent reader that gets the data, not the oldest reader).

Right now, I have this crappy check to see if a named pipe is being read from:

is_named_pipe_being_read() {

    local named_pipe="$1" 
    echo "unlocked" > "$named_pipe" & 
    pid="$!"
    # Wait a short amount of time
    sleep 0.25
    # Kill the background process. If kill succeeds, then
    # the write was blocked 
    ( kill -PIPE "$pid" ) &> /dev/null
}

if the kill works (exits with 0) then it means that nobody was reading from the pipe.

But instead of having a 0.25 second delay and starting up an unnecessary process, I am looking for a way to check the named pipe to see if it's opened for reading? Is there some way to determine if something is reading from it?

Note: I can't read from the pipe in this call, I can only write to it (because with named pipes the order at which readers are attached does not seem to be respected).

Right now, I have this crappy check to see if a named pipe is being read from:

is_named_pipe_being_read() {

    local named_pipe="$1" 
    echo "unlocked" > "$named_pipe" & 
    pid="$!"
    # Wait a short amount of time
    sleep 0.25
    # Kill the background process. If kill succeeds, then
    # the write was blocked 
    ( kill -PIPE "$pid" ) &> /dev/null
}

if the kill works (exits with 0) then it means that nobody was reading from the pipe.

But instead of having a 0.25 second delay and starting up an unnecessary process, I am looking for a way to check the named pipe to see if it's opened for reading? Is there some way to determine if something is reading from it?

Note: I can't read from the pipe in this call, I can only write to it (because with named pipes the order at which readers are attached does not seem to be respected - or perhaps it's the most recent reader that gets the data, not the oldest reader).

Source Link
Alexander Mills
  • 10.9k
  • 27
  • 120
  • 214

Check if named pipe is open for reading

Right now, I have this crappy check to see if a named pipe is being read from:

is_named_pipe_being_read() {

    local named_pipe="$1" 
    echo "unlocked" > "$named_pipe" & 
    pid="$!"
    # Wait a short amount of time
    sleep 0.25
    # Kill the background process. If kill succeeds, then
    # the write was blocked 
    ( kill -PIPE "$pid" ) &> /dev/null
}

if the kill works (exits with 0) then it means that nobody was reading from the pipe.

But instead of having a 0.25 second delay and starting up an unnecessary process, I am looking for a way to check the named pipe to see if it's opened for reading? Is there some way to determine if something is reading from it?

Note: I can't read from the pipe in this call, I can only write to it (because with named pipes the order at which readers are attached does not seem to be respected).