0

I want to show messages from a logfile in realtime on desktop. (xfce4 on fedora 24)

My idea is to do this by using notify-send and tail -f in a shell script.

So far I have two shell scripts:

  1. read_data.sh
  2. write_data.sh

Both fork a process and communicates via a pipe.

write_data.sh:

tail -f /var/log/logfile > mypipe

read_data.sh:

mkfifo mypipe

while true
do
    echo "read now from pipe"
    if read line <mypipe; then
        echo $line
    fi
done

Unfortunately I get an error message:

EPIPE (Broken pipe)

I used strace to analyze what's going on:

write_data.sh:

strace tail -f /var/log/logfile > mypipe
....
write(1, "Message from logfile"..., 281) = -1 EPIPE (Broken pipe)
--- SIGPIPE {si_signo=SIGPIPE, si_code=SI_USER, si_pid=7314, si_uid=0} ---
+++ killed by SIGPIPE +++


strace read_data.sh
...
read(0, "\n", 1)                        = 1
dup2(10, 0)                             = 0
fcntl(10, F_GETFD)                      = 0x1 (flags FD_CLOEXEC)
close(10)                               = 0
open(".", O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFDIR|0550, st_size=4096, ...}) = 0
getdents(3, /* 133 entries */, 32768)   = 5400
getdents(3, /* 0 entries */, 32768)     = 0
close(3)                                = 0
write(1, "message from logfile ....
) = 62
rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
write(1, "read now from pipe\n", 19read now from pipe
)    = 19
open("/tmp/mypipe", O_RDONLY

read_data.sh blocks at this point.

Any idea why this happens?

0

The SIGPIPE is caused by the fact that your reading loop opens and closes the FIFO file for reading at every iteration. Try this instead:

while read line; do
    echo "$line"
done <"$pipe"
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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