Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://94327]
help
Chatterbox?
[Corion]: Also, I think the OPs problem is on their intranet and not on the internet (also, they should likely use WWW::Mechanize instead, which knows about cookie)
[1nickt]: yes, could be.
[1nickt]: I don;t see the link , definitely, after reloading the node
[1nickt]: Is it possible to disable it?
[Discipulus]: if you mean [download] after the code it is there 1nickt
[1nickt]: yes, Corion sees it too, and I see it when not logged in.
[Corion]: I think you can disable download links in your settings?
[1nickt]: Looking in settings to see if I disabled it in my profile ...
[1nickt]: There is "Don;t show embedded d/l links" in Display settings. It is unchecked.
[1nickt]: Oh, there is a minimum lines setting!

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2017-05-23 18:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?