in reply to Emulating command line pipe

Don't use system - if you just open a pipe from tac to yourself, you have full control.
my $tac_pid = open my $fh, "tac $logfile |"; while (<$fh>) { last if /$match/; } kill 13, $tac_pid; # SIGPIPE close $fh;

But even so tac might be reading more than it should needed, since there's buffering on both sides of the pipe (and yes, I guess closing the filehandle $fh sends a SIGPIPE to tac... :-)

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Replies are listed 'Best First'.
Re^2: Emulating command line pipe
by davistv (Acolyte) on Aug 31, 2007 at 17:08 UTC
    Thank you, shmem. Unfortunately once run as a cgi the process is no longer killed upon a match, I'm not sure why. I think this might be an issue with apache, but I'm not certain.

    Thank You,
    Troy
      AFAIK grep -m 1 exiting causes the OS to send a SIGPIPE to tac upon grep exit... hmm. Could it be that SIGPIPE is masked somewhere in mod_perl, ? is that masking propagated to the child? Is it even possible to mask a signal for a process group? Now that's something I don't know, you got me there :-)

      If that's the case - i.e. if SIGPIPE is masked - try my solution (which doesn't involve grep) and kill off tac with another signal (15 or, if it's renitent, 9 - SIGKILL).

      --shmem

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
        That was it, changed the signal to 9 and the script now executes in about 10 seconds. Thanks!

        Cheers,
        Troy