deprecated has asked for the wisdom of the Perl Monks concerning the following question:

I am writing a killall script in perl to replace the unix killall command. I'd like it to be pure perl. I have written a shell script thats rather simple itself and probably not terribly difficult to replace in perl.
for i in `ps -xawu | grep "$*" | grep -v grep | awk '{ print $2 }'`; d +o kill -9 $i ; done ;;
just looking at it, its hurting for a foreach (@pids) { ... } and a split, and so on.

thanks,
dep.

Replies are listed 'Best First'.
Re: How do I get the data
by Fastolfe (Vicar) on Feb 01, 2001 at 03:32 UTC
Re: How do I get the data
by turnstep (Parson) on Feb 01, 2001 at 02:06 UTC

    A quick note: You should probably use a kill -15, not a kill -9. Better still, have 15 be the default and allow the first argument to override it - just like the killall command does.

    And, of course, make sure that your perl script uses a positive 15 and not a negative one!

Re: How do I get the data
by runrig (Abbot) on Feb 01, 2001 at 02:28 UTC
    Kill takes a list, so you could just do:
    kill $signal, `ps -xawu | ...`; or kill $signal, @pids; # Once you have the pids
    BUT,
    I don't have option 'x' for my ps, so I don't know what it does, but if I'm killing a user, I'd use Getopt::Std to accept a user argument and do a 'ps -u $username', and if I'm killing a certain command, I'd split the output and only kill pids where the command 'eq'uals the command I'm looking for, (or use -t for a tty etc). Grepping is too scary to me when killing processes.

    And ++ for turnstep's suggestion about using signal 15 for the default, and making it an option.
Re: How do I get the data
by deprecated (Priest) on Feb 01, 2001 at 03:18 UTC
    It appears I cannot update this post. My subject was chopped off.

    Im trying to get the data that ps does without actually using ps. I wanted to write a pure-perl killall command, and hopefully not use any modules.

    --
    i am not cool enough to have a signature.