Why does not tail exit here?

Why does not tail exit here?

am 20.12.2007 00:26:14 von Mikhail Teterin

Why does not the script below exit properly?

#!/bin/sh

if tail -f /var/log/messages | awk '{print "Exiting"; exit 0}'
then
echo Exited
else
echo Failed
fi

exit 0

Awk, actually, exits, but tail stays around. On BSD and Linux the entire
sh-script waits. On Solaris the sh-script completes, but the tail is left
hanging in the background -- forever.

Why does not tail exit automatically, when its stdout is closed? Thanks!

-mi

Re: Why does not tail exit here?

am 20.12.2007 01:38:54 von Barry Margolin

In article ,
Mikhail Teterin wrote:

> Why does not the script below exit properly?
>
> #!/bin/sh
>
> if tail -f /var/log/messages | awk '{print "Exiting"; exit 0}'
> then
> echo Exited
> else
> echo Failed
> fi
>
> exit 0
>
> Awk, actually, exits, but tail stays around. On BSD and Linux the entire
> sh-script waits. On Solaris the sh-script completes, but the tail is left
> hanging in the background -- forever.
>
> Why does not tail exit automatically, when its stdout is closed? Thanks!

It should exit the next time something is written to /var/log/messages.
At that time it will try to write it to the pipe, and get an error.

But it's not doing anything until then, so it doesn't notice that the
pipe has closed.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***

Re: Why does not tail exit here?

am 20.12.2007 01:40:47 von Mikhail Teterin

Barry Margolin wrote:
> It should exit the next time something is written to /var/log/messages.
> At that time it will try to write it to the pipe, and get an error.
>
> But it's not doing anything until then, so it doesn't notice that the
> pipe has closed.

Does not it get a SIGPIPE or something? Should not it? Thanks!

-mi

Re: Why does not tail exit here?

am 20.12.2007 01:56:53 von Barry Margolin

In article ,
Mikhail Teterin wrote:

> Barry Margolin wrote:
> > It should exit the next time something is written to /var/log/messages.
> > At that time it will try to write it to the pipe, and get an error.
> >
> > But it's not doing anything until then, so it doesn't notice that the
> > pipe has closed.
>
> Does not it get a SIGPIPE or something? Should not it? Thanks!

SIGPIPE occurs when you try to write to a pipe whose reading end has
closed. If you don't have anything to write, you don't get a signal.

That's why tail doesn't exit until something new is written to
/var/log/messages.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***