Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Killing clones for fun and profit

by PrakashK (Pilgrim)
on Jul 06, 2001 at 02:57 UTC ( #94327=note: print w/ replies, xml ) Need Help??

in reply to Killing clones for fun and profit

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; }

Comment on Re: Killing clones for fun and profit
Select or Download Code

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://94327]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2015-11-26 12:33 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (698 votes), past polls