Instead of:
my @results = `ps -ef`;
you could do:
my @results = `ps -fu $user`;
This should give you only your processes and eliminates one of your filters.

TO check if the parent process is a shell or not, check the command part of the process info, which should be the basename of your $SHELL value. If not, you would have to go for its parent until you find the real shell.

Also, I would use a hash instead of an array for the list of process hashrefs.

Anyway, here is some (untested) changes to your code:

#!/usr/bin/perl -w use strict; my $prog = (split "\/", $0)[-1]; die "Usage: $prog\n" if @ARGV; my $user = getpwuid $<; my @results = qx{/bin/ps -fu $user}; chomp @results; shift @results; # remove header my %listing; for (@results) { if (m|^([\w\d]+) \s+ (\d+) \s+ (\d+) \s+ (\d+) \s+ ([\w\d:]+) \s+ ([\w\d/?]+) \s+ ([\w\d:]+) \s+ (.*)$|x) { my $hashref; $$hashref{'uid'} = $1; $$hashref{'pid'} = $2; $$hashref{'ppid'} = $3; $$hashref{'c'} = $4; $$hashref{'stime'} = $5; $$hashref{'tty'} = $6; $$hashref{'time'} = $7; $$hashref{'cmd'} = $8; $listing{$pid} = $hashref; } else { warn "$prog: Could not process line! Line follows:\n$_\n"; next } } my ($shell) = ($ENV{SHELL} =~ m{([^/]*)$}; # get the basename of the s +hell my $pid = $$; my $ppid = $listing{$pid}->{ppid}; while ($listing{$ppid}->{cmd} !~ m/$shell$/;) { $pid = $ppid; $ppid = $listing{$pid}->{ppid}; } # $ppid should have the current shell's pid for my $pid (keys %listing) { kill 9, $pid unless $listing{$pid}->{cmd} =~ m/^-/ and $pid != $ppid; }
/prakash

In reply to Re: Killing clones for fun and profit by PrakashK
in thread Killing clones for fun and profit by CheeseLord

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.