Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://94327]
[marto]: Discipulus concerned partys should report things. as tye mentioned in his post (linked in the other thread from yesterday) there is an approach and strategy for such things
[Discipulus]: which node yesterday?
[Discipulus]: i cannot see any link to tye post..

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2017-06-23 08:48 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (538 votes). Check out past polls.