At 09:37 AM 7/8/02 -0500, Daryl J. Hoyt wrote:
>Hi,
>     I am trying to write a script to kill all processes named $string.

Does your system not have killall?

NAME
        killall - kill processes by name

SYNOPSIS
        killall [-e,--exact] [-g,--process-group] [-i,--interac­
        tive] [-q,--quiet] [-v,--verbose] [-w,--wait] [-V,--ver­
        sion] [-s,--signal signal] [--] name ...
        killall -l
        killall -V,--version

DESCRIPTION
        killall sends a signal to all processes running any of the
        specified  commands.  If  no  signal  name  is  specified,
        SIGTERM is sent.
[...]

>I am
>not sure how to handle the regex the right way.  Here is a sample of the
>command:
>
>djh      17893 17892  0 Jul03 pts/16   00:00:00 rlogin test3
>djh       6401 25628  0 Jul05 ?        00:00:00 [rhn-applet <defunct>]
>djh       6525 25746  0 Jul05 pts/1    00:00:00 bash
>djh       6530  6525  0 Jul05 pts/1    00:00:00 rlogin xeon
>djh       6531  6530  0 Jul05 pts/1    00:00:00 rlogin xeon
>djh       6828 25746  0 Jul05 pts/9    00:00:00 bash
>djh       6833  6828  0 Jul05 pts/9    00:00:00 rlogin xeon
>djh       6834  6833  0 Jul05 pts/9    00:00:11 rlogin xeon
>djh       7292 26480  0 Jul05 pts/10   00:00:00 rlogin spot
>djh       7293  7292  0 Jul05 pts/10   00:00:00 rlogin spot
>djh       7361 25746  0 Jul05 pts/12   00:00:00 bash
>djh       7366  7361  0 Jul05 pts/12   00:00:00 rlogin spot
>djh       7367  7366  0 Jul05 pts/12   00:00:00 rlogin spot
>djh       7466 25746  0 Jul05 pts/14   00:00:00 bash
>djh       1612 25628  0 08:49 ?        00:00:00 [rhn-applet <defunct>]
>djh       1639 25746  0 08:50 pts/17   00:00:00 bash
>djh       1647  1639  0 08:51 pts/17   00:00:00 rlogin duke
>djh       1648  1647  0 08:51 pts/17   00:00:00 rlogin duke
>djh       1742     1  0 09:10 ?        00:00:00 gvim process_killer.pl
>djh       1778  7466  0 09:20 pts/14   00:00:00 /usr/local/bin/perl
>../process_kidjh       1779  1778  0 09:20 pts/14   00:00:00 sh -c ps -ef |
>grep djh
>djh       1780  1779  0 09:20 pts/14   00:00:00 ps -ef
>djh       1781  1779  0 09:20 pts/14   00:00:00 grep djh
>
>I want to strip out the fist columns of numbers (the PIDs).  How would I do
>this if each line of this is an element in an array?  Any help would be
>appreciated.

Just parse it a line at a time.  Split the line on white space; the pid 
is the second field.  The command may contain white space but nothing 
before it can, so get the command by joining on space everything from 
the first column of the command thru the last column.

for (`$PS`) {
   my @cols = split;
   my $pid = $cols[1];
   my $command = join ' ' => @cols[7 .. $#cols];
   kill TERM => $pid if $command eq $string;   ###
}

Might as well make it so you can use a regex in $string.  Say that if 
$string is enclosed in /.../, it means it's a regex.  Replace the ### line with

   my $regex = $string;
   kill TERM => $pid if ($regex =~ s#^/(.+)/$##) ? $command =~ /$regex/
  : $command eq $string;

Um, maybe that's a bit abbreviated for a beginners' list :-)  Alternatively:

   my $killit;
   my $regex = $string;
   if ($regex =~ s#^/(.+)/$##) {
     $killit = $command =~ /$regex/;
   }
   else {
     $killit = $command eq $string;
   }
   kill TERM => $pid if $killit;

I suppose there might be some attraction to killing all the processes 
at once, in which case instead of killing them, you add the $pid to an 
array @pids and once done with the loop do 'kill TERM => @pids if @pids'.
--
Peter Scott
Pacific Systems Design Technologies
http://www.perldebugged.com/


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to