CheeseLord has asked for the wisdom of the Perl Monks concerning the following question:
Like many other users of Windows, my computer crashes frequently. I've gotten to the point where I don't really mind it all that much, but I spend a great deal of time logged into my account at school from my home computer, and when my comp crashes, I remain logged in. I know after a while these logins will (hopefully) be killed off, but I'm impatient. So I harnessed the power of Perl to find a way to kill these clones so that I don't appear to have 14 terminals open at a time. My code follows:
#!/usr/bin/perl -w use strict; my $prog = (split "\/", $0)[-1]; die "Usage: $prog\n" if @ARGV; my $user = getpwuid $<; my @results = `ps -ef`; 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; push @listing, $hashref; } else { warn "$prog: Could not process line! Line follows:\n$_\n"; next } } # My thinking: when run from the shell, this process' ppid will be the # pid of my shell. Problem is, what happens when it's not run from the # shell (say, run through vi's ":!killclone")? my $ppid = (grep {$$_{'pid'} eq $$} @listing)[0]->{'ppid'}; @listing = grep {$$_{'uid'} eq $user} @listing; # Just mine @listing = grep {$$_{'cmd'} =~ /^-/} @listing; # Just login shells @listing = grep {$$_{'pid'} ne $ppid} @listing; # Not this shell for (@listing) { kill 9, $$_{'pid'} }
It seems to be working fine, but there is the problem I note in the comments above "my $ppid". I'd like to know how to solve that, as well as any of your suggestions on other things I should have done, or places my logic is just shot. Also, I know that the dot-star in the regex isn't looked upon all that highly here at PM, but I think that it's the right tool for this, as I really do want everything to the end of the line... I think. :) Anyway, thanks in advance for your help.
Note: While I appreciate the awesomeness of CPAN, CPAN modules would be a less-than-ideal solution, as I don't have access to do a real install, and I don't have all that much room on this account. But, if it would significantly improve things, I'm always open to clearing space for a module.
His Royal Cheeziness
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Killing clones for fun and profit
by tadman (Prior) on Jul 06, 2001 at 00:52 UTC | |
|
Re: Killing clones for fun and profit
by blakem (Monsignor) on Jul 06, 2001 at 02:53 UTC | |
by Hero Zzyzzx (Curate) on Jul 06, 2001 at 02:58 UTC | |
|
Re: Killing clones for fun and profit
by PrakashK (Pilgrim) on Jul 06, 2001 at 02:57 UTC |