#!/usr/bin/env perl use strict; use warnings; use Getopt::Std; use POSIX qw(uname); my %opt; getopts( 'chk', \%opt ); $opt{h} and HELP_MESSAGE(); my $onwin = do { my @uname = uname(); shift(@uname) =~ /^CYGWIN/ }; open( my $ps, '-|', 'ps', $onwin ? '-efW' : '-ef' ) or die "cannot run ps: $!\n"; my $procname_rx = join( '|', @ARGV ); $procname_rx = qr/$procname_rx/i; my @headers = split( ' ', <$ps> ); my $nrheaders = scalar(@headers); while (<$ps>) { #warn "ps output line is $_"; my ( $user, $pid, @rest ) = split( ' ', $_, $nrheaders ); my $cmdline = pop(@rest); $cmdline =~ s/^\d+\s+//; # if it was the 12 in Sep 12 chomp($cmdline); #warn "pid is $pid\n"; #warn "cmdline is $cmdline\n"; if ( $opt{c} ) { # only match against the command name $cmdline =~ s#.*[\\/]##; $cmdline =~ s#\.exe$##i if $onwin; } if ( $pid != $$ && $cmdline =~ /$procname_rx/ ) { print; if ( $opt{k} ) { my @kill = ( '/bin/kill', $onwin ? '-f' : '-9', $pid ); #warn join(' ', @kill), "\n"; system(@kill) == 0 or warn "kill failed: $@\n"; if ($?) { warn "could not kill process $pid\n"; } } } else { #warn "$cmdline does not match $procname_rx\n"; } } close($ps); sub HELP_MESSAGE { print <