Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Killer

by ergowolf (Monk)
on Aug 04, 2000 at 18:56 UTC ( [id://26200]=sourcecode: print w/replies, xml ) Need Help??
Category: Unix utilitiy
Author/Contact Info ergowolf
Description: replace the word process with what you are looking for. I used this script to kill a particularly tough process at work. I am sure this is the hard way, but its how I did it and it works. I would like to see the easy way though.
!usr/local/bin/perl use strict; my @processes=`ps -ef | grep process | awk \'{ print \$2 }\'`; foreach (@processes) { `kill -9 $_` && print "Process $_ killed!"; }
Replies are listed 'Best First'.
RE: Killer
by tye (Sage) on Aug 04, 2000 at 19:26 UTC

    Using /bin/grep, awk, and /bin/kill inside Perl! Blasphemy!!! Friar, where is the hot oil?? ;)

    #!/usr/bin/perl -w use strict; die "Usage: $0 regex\n" unless 1 == @ARGV; for my $proc ( map {(split(' '))[1]} grep /$ARGV[0]/, `ps -ef` ) { if( ! kill(-9,$proc) ) { warn "Can't kill PID $proc: $!\n"; } else { print "Killed PID $proc.\n"; } }

    And that still leaves much room for improvement (and I didn't test this either, sorry).

      I'm never sure why people bring out the flamethrower to light a candle. I have a little rant on "please stop using kill -9" that I post to comp.unix.questions when I see postings like this. Basically, -9 is harmful because it doesn't give the process a chance to clean up. Try a 2, a 1, and a 15, waiting 1 second each time. If all those fail, then a 9.

      Also, -9 is "kill 9 to everyone in the process group". You want +9, certainly!

      -- Randal L. Schwartz, Perl hacker

        Actually, that is one of my pet peeves, too. It has been a long time since I've seen someone type "kill" at a Unix shell prompt, so I've become desensitize (too much Win32 -- also part of why I didn't test this).

        Also, thanks for catching the too-literal translation of /bin/kill to kill().

                -- tye (but my friends call me "Tye")
        Merlyn has got your number ergowolf.

        -9 like using Nitro. Fun, but not productive.

        I like to use the applications normal shutdown procedure, if possible and if it has one

        Then kill -15 - "please shut down now little process" then kill -2 - "come on and shut down now." then and only then kill -9 "die, die vile process!!"

RE: Killer
by ergowolf (Monk) on Aug 31, 2000 at 18:17 UTC
    tye
    "Using /bin/grep, awk, and /bin/kill inside Perl! Blasphemy! !! Friar, where is the hot oil?? ;)"

    You are right and this is funny. :)

    "And that still leaves much room for improvement (and I didn't test this either, sorry)."

    I find this one somewhat insulting. I came here to learn. If you read my description maybe you would have been more kind. I posted it bellow so you could read it.

    I am sure this is the hard way,but its how I did it and it works. I would like to see the easy way though.

    merlyn
    "I'm never sure why people bring out the flamethrower to light a candle. I have a little rant on "please stop using kill -9" that I post to comp.unix.questions when I see postings like this. Basically, -9 is harmful because it doesn't give the process a chance to clean up. Try a 2, a 1, and a 15, waiting 1 second each time. If all those fail, then a 9.

    Also, -9 is "kill 9 to everyone in the process group". You want +9, certainly!"

    I used kill 9, because it is a poorly written stubborn process that will not die any other way. You make some good points.

    Ergowolf
    Does code make a sound if no one is there to type it?

      "And that still leaves much room for improvement (and I didn't test this either, sorry)."

      I find this one somewhat insulting. I came here to learn. If you read my description maybe you would have been more kind. I posted it bellow so you could read it.

      I'm terribly sorry. In hind site I see where that would come off as insulting. I was trying to refer to my code that I had just thrown in very quickly and which had much room for improvement (and wasn't tested). I did not mean to imply criticism of your code, especially such harsh criticism.

      I don't mind harshly criticizing my own code. But in my zeal to save face by criticizing my own code before anyone else could I inadvertantly was very rude. Again, I'm sorry for that. I'll try to be more careful in future.

      And thanks for pointing this out. It is often easier to just mutter "a**hole" and walk away. I usually try hard to not be an a**hole but am still frustrated to realize that I never the less am one sometimes.

      So I offer a belated "Welcome!" to the Monastery. It is a fine place to learn and most of us aren't jerks most of the time. (:

              - tye (but my friends call me "Tye")
        I understand and accept. :) I can see your point of view on this. I gave you a point for the last post. I will never remain silent if something is on my mind. I have been on perlmonks for a long time. I just haven't visited lately. I have been playing video games, working, etc. My perl skills are not that great, but I try to dabble.

        Ergowolf Does code make a sound if no one is there to type it?
RE: Killer
by AgentM (Curate) on Sep 28, 2000 at 08:52 UTC
    Actually, the POSIXly correct way of sending a signal is with the use of sigqueue() which should give some interesting benefits over your generic kill(). I realize that it hasn't really caught on but considering that I'm supposed to be a POSIX professional programmer dude, I thought i just might share that with you. Anyway, I DO use sigqueue because the receiver gets a nifty struct of info (especially useful with IPC, specifically mesgqs). Just a quick comment...

      I'm curious about how this "POSIXly correct way" would translate into Perl code. I've actually recently written something to use in sort of a panic situation to get rid of a runaway process using something akin to kill('TERM', $pid); and would like to know how that matches up with notions of POSIX correctness, if there's any comparison to be made at all.

      print substr("Just another Perl hacker", 0, -2);
      - apotheon
      CopyWrite Chad Perrin

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (8)
As of 2024-03-29 14:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found