Skip to content

Instantly share code, notes, and snippets.

@pmakholm
Created August 27, 2009 08:10
Show Gist options
  • Save pmakholm/176170 to your computer and use it in GitHub Desktop.
Save pmakholm/176170 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl;
use strict;
use warnings;
# Simple 'head -n N' implementation:
use Getopt::Long;
my $lines = 10;
GetOptions(
"lines|n=i" => \$lines,
);
while ($lines--) {
my $line = <>;
print $line;
}
# and now do some shooting...
# Find producers. For the example just care about people producing to STDOUT
no warnings 'uninitialized'; # readlink *will* return a lot of undefs
my $pipe = readlink "/proc/$$/fd/0";
my @pids = grep { readlink("/proc/$_/fd/1") eq $pipe } map { s!^/proc/!!; $_ } glob "/proc/[0-9]*";
print STDERR "Killing: ", join(", ", @pids), "\n" if $ENV{DEBUG};
# Kill the producers
kill PIPE => @pids;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment