Mailing List

Flat
Closing a pipe
User: mcholste
Date: 4/4/2012 4:05 pm
Views: 435
Rating: 0
Any idea why the following works for INT via CTRL+C but not TERM via
kill?  With TERM, it prints "Shutting down" but not "pipe closed."

my $pipe;
open($pipe, "-|", "find /var/log -type f | xargs tail -f -n 0");
$SIG{INT} = $SIG{TERM} = sub {
       print "Shutting down\n";
       close($pipe);
       print "pipe closed\n";
       exit;
};

while (<$pipe>){
       print $_;
}
Re: Closing a pipe
User: afbach
Date: 4/4/2012 5:10 pm
Views: 0
Rating: 0
On Wed, Apr 4, 2012 at 4:05 PM,   wrote:
>  With TERM, it prints "Shutting down" but not "pipe closed."

Check the value/something of close() - maybe it dies first as the
pipe's already closed?

--

a

Andy Bach,
afbach@gmail.com
608 658-1890 cell
608 261-5738 wk
Re: Closing a pipe
User: afbach
Date: 4/5/2012 11:25 am
Views: 0
Rating: 0
On Wed, Apr 4, 2012 at 4:05 PM,   wrote:
> Any idea why the following works for INT via CTRL+C but not TERM via
> kill?  With TERM, it prints "Shutting down" but not "pipe closed."

Sorry should've tried it first - I see both:
andy@wiwmb-afb-ub1:~/spam$ pipe_sig.pl&
...
==> /var/log/nagios2/archives/nagios-10-28-2008-00.log <==

==> /var/log/nagios2/archives/nagios-09-13-2008-00.log <==

==> /var/log/nagios2/archives/nagios-09-06-2008-00.log <==

==> /var/log/nagios2/archives/nagios-06-21-2008-00.log <==

kill -TERM %1
andy@wiwmb-afb-ub1:~/spam$ Shutting down
pipe closed

[1]+  Done                    pipe_sig.pl
andy@wiwmb-afb-ub1:~/spam$

did have to modify the path:
#!/usr/bin/perl

my $pipe;
open( $pipe, "-|", "find /var/log/nagios2 -type f | xargs tail -f -n 0" );
$SIG{INT} = $SIG{TERM} = sub {
   print "Shutting down\n";
   close($pipe);
   print "pipe closed\n";
   exit;
};

while (<$pipe>) {
   print $_;
}

otherwise I got a tail error
tail: option used in invalid context -- 5

--

a

Andy Bach,
afbach@gmail.com
608 658-1890 cell
608 261-5738 wk
Re: Closing a pipe
User: mcholste
Date: 4/5/2012 2:08 pm
Views: 0
Rating: 0
Ok, $pipe->blocking(0) seems to work fine, it's $pipe->close() that
doesn't work, and it only doesn't work under $SIG{ALRM} for some
reason.  All works as expected under TERM and INT.

On Thu, Apr 5, 2012 at 11:25 AM,   wrote:
> afbach wrote:
>
> On Wed, Apr 4, 2012 at 4:05 PM,   wrote:
>> Any idea why the following works for INT via CTRL+C but not TERM via
>> kill?  With TERM, it prints "Shutting down" but not "pipe closed."
>
> Sorry should've tried it first - I see both:
> andy@wiwmb-afb-ub1:~/spam$ pipe_sig.pl&
> ...
> ==> /var/log/nagios2/archives/nagios-10-28-2008-00.log <==
>
> ==> /var/log/nagios2/archives/nagios-09-13-2008-00.log <==
>
> ==> /var/log/nagios2/archives/nagios-09-06-2008-00.log <==
>
> ==> /var/log/nagios2/archives/nagios-06-21-2008-00.log <==
>
> kill -TERM %1
> andy@wiwmb-afb-ub1:~/spam$ Shutting down
> pipe closed
>
> [1]+  Done                    pipe_sig.pl
> andy@wiwmb-afb-ub1:~/spam$
>
> did have to modify the path:
> #!/usr/bin/perl
>
> my $pipe;
> open( $pipe, "-|", "find /var/log/nagios2 -type f | xargs tail -f -n 0" );
>
> $SIG{INT} = $SIG{TERM} = sub {
>    print "Shutting down\n";
>    close($pipe);
>    print "pipe closed\n";
>    exit;
> };
>
> while (<$pipe>) {
>    print $_;
> }
>
> otherwise I got a tail error
> tail: option used in invalid context -- 5
>
>
> --
>
> a
>
> Andy Bach,
> afbach@gmail.com
> 608 658-1890 cell
> 608 261-5738 wk
>
> View Online
>
>
>
> Madison Area Perl Mongers - MadMongers
> http://www.madmongers.org
PreviousNext
Madison Area Perl Mongers