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


In reply to 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.