Find the answer to your Linux question:
Results 1 to 9 of 9
I have been trying to figure this out for some time now, and I am just about at my wits end. So I am asking for help.. I have a ...
  1. #1
    Just Joined!
    Join Date
    Aug 2007
    Posts
    1

    Question Need help with killing multiple PIDs..

    I have been trying to figure this out for some time now, and I am just about at my wits end. So I am asking for help..

    I have a cluster, that when a job is finished, is not killing all the left over processes. So I am left with something like this.
    6652 pts/2 00:00:00 bash
    6674 pts/3 00:00:00 bash
    6753 pts/3 00:00:00 magma3234
    6754 pts/3 00:00:00 magma23424
    6755 pts/3 00:00:00 magma493844
    6756 pts/3 00:00:00 magma3954755
    6757 pts/3 00:00:00 ps

    I need the script to be able to pick up on those random process names and killall -9 them.

    Somebody, please help me.

    Joshua

  2. #2
    Linux Newbie
    Join Date
    Dec 2005
    Location
    Toronto
    Posts
    127
    Though this not a solution to your problem, I thought I'd mention something that came up my mind.

    Logically thinking, when a process is finished processing it should terminate automatically and cleanly. If you have many processes lurking around even after you think the job is finished you might wanna find out what is going on. You wouldn't want to corrupt data by killing the processes, they might just be waiting for an event to happen or for I/O, not sure !

    If you think this is irrelevant, just ignore it !

  3. #3
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    The real answer is to fix the job so that it's not leaving around a bunch of orphaned processes.

    Assuming you're referring to the processes that begin with 'magma', you can kill them with:
    # pkill 'magma'

    It's possible to fine-tune that command significantly (using regexps) if the above is casting too wide a net.

  4. #4
    Linux Engineer Freston's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    1,047
    Funny thing, it had taken me quite some time before I realized ctrl-z doesn't end a process, it just stops it and puts it in the background. Don't know if something like this is the case in your situation though. Just thought I'd mention it.

    Anyway. Because of this, I made a really devastating script. You can have it, but use it with care.

    Code:
    #!/bin/bash
    for i in `ps aux | grep -i $1 | grep -v grep | awk '{print $2}'`; do
    kill -9 $i
    done
    exit 0
    This script is old, it's UGLY, it's WAY to complex, and it can mess up your system. Still, it does the job like a laser guided H-bomb.


    It just kills everything it would find when you'd <ps aux | grep -i FOO>
    That means you don't have to write out the entire name, making it quite useful when you're lazy. Some programs do file bug reports when you use this script on them though (sorry people of Firefox and OpenOffice.org, it was me )
    Can't tell an OS by it's GUI

  5. #5
    Linux Newbie
    Join Date
    Dec 2005
    Location
    Toronto
    Posts
    127
    #!/bin/bash
    for i in `ps aux | grep -i $1 | grep -v grep | awk '{print $2}'`; do
    kill -9 $i
    done
    exit 0
    Don't you think the above script is likely to kill itself. Well, it's a different matter if gets killed just when it's done its work !!

  6. #6
    Linux Engineer Freston's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    1,047
    Quote Originally Posted by kevkim55
    Don't you think the above script is likely to kill itself. Well, it's a different matter if gets killed just when it's done its work !!
    That's where the grep -v grep comes in
    Can't tell an OS by it's GUI

  7. #7
    Linux Newbie
    Join Date
    Dec 2005
    Location
    Toronto
    Posts
    127
    That's where the grep -v grep comes in
    That would filter out the grep but not the comand line itself !

    If we call the script 'killer-script.sh' and execute it like this:

    killer-script.sh dummy-prog

    'grep -v grep' would filter out the grep - 'grep -i $1' !

    Output of 'grep -i $1' would also include the command/process 'killer-script dummy-prog' !

    Let me know if I'm wrong !

  8. #8
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    What kevkim55 is saying is given:
    ./script_name argument

    The process table will contain 'argument', so any grep that seeks to match 'argument' will grab the script's entry/PID (which will be killed -- possibly before it finishes its job).

    Just use pkill and make your life easy.

  9. #9
    Linux Engineer Freston's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    1,047
    Quote Originally Posted by kevkim55
    That would filter out the grep but not the comand line itself !

    If we call the script 'killer-script.sh' and execute it like this:

    killer-script.sh dummy-prog

    'grep -v grep' would filter out the grep - 'grep -i $1' !

    Output of 'grep -i $1' would also include the command/process 'killer-script dummy-prog' !

    Let me know if I'm wrong !
    Hey! you're right!
    Ain't that something? The first ever script that works while it shouldn't. And I wrote it! Will I be famous? Should I go into development?



    ===============


    To tell you the truth, you where right all along. The script cycles through all the matches and kills them. When it finally finds itself, it's already done. It's always the last in line. No use putting an exit in it then, but for the rest it works. Told ya it was UGLY
    Can't tell an OS by it's GUI

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...