Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Killing multiple logins.

by PrimeLord (Pilgrim)
on Apr 23, 2002 at 20:58 UTC ( [id://161440]=note: print w/replies, xml ) Need Help??


in reply to Killing multiple logins.

So you don't end up killing something you didn't really want to you could do something interactive like this.

use strict; open PS, "ps |" or die "$!"; while (<PS>) { chomp; my @process = split; next if $process[0] =~ /[^\d]/; print "Would you like to kill the following process?\n"; print "$_\n"; chomp (my $ans = <STDIN>); kill 9 => $process[0] if $ans eq 'yes'; } close PS;


Its quick, dirty, and untested but it should provide the funtionality you are looking for. You can have it run out of your .bash_profile or your shells equivalent or just run it manually whenever you want.

Update: I added the next if $process[0] =~ /[^\d]/; line so it doesn't prompt you to kill the header in the ps listing.

HTH

-Prime

Replies are listed 'Best First'.
Re: Re: Killing multiple logins.
by agentv (Friar) on Apr 24, 2002 at 01:18 UTC

    ...the problem I have with this approach (and with the use of who(1)) is that their output is not predictable. Some people still use the BSD version of ps(1), some people use the SysV version, and some use the GNU version. I presume that on Solaris, you're using the SysV version unless you've updated your path.

    Given that assumption, it is possible to use Perl easily enough to parse the output and give you the identities of the processes you hope to kill. And that would look a lot like the classic shell script that does this task.

    I keep thinking (as I write this) that there ought to be a nice one-liner that does the trick. Perhaps I'll work it out.

    ---v

    Well, I took a crack at it, but the only improvements I was able to offer is an invocation of ps(1) that renders the output a little more predicatably.

    Here's the code:

    #!/usr/bin/perl $myself = "student"; chomp ($mytty = `tty`); # gets: /dev/pts/5 $mytty =~ s/.dev.//; # gets: pts/5 =preview We'll run the command: ps -u $myself -o 'pid,tty' It will produce output like this: PID TT 419 pts/3 401 pts/3 398 ? 399 pts/3 437 ? 479 pts/5 513 pts/5 1175 ? =cut @lines = split /\n/, `ps -u $myself -o 'pid,tty'`; shift @lines; # gets rid of header line foreach $entry (@lines) { ($pid, $term) = $entry =~ /^\D*(\d+)\s+(\S+)/; next if ($term =~ /^[?]/ || $term eq $mytty); print "kill 9, $pid\n"; # for testing # kill 9, $pid; # production version }

    With light testing on this system (sol8,perl v5.005), it gave the results I was hoping for. I don't know if there are some anomalous listings that might have to be accounted for.

    But you will note that it finds processes with a TTY other than the current one, and kills them all. It would be better to kill only the process group leader and that would cause the others to be killed by init.

    YMMV

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-03-29 05:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found