#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    10
    Rep Power
    0

    Find and kill a program


    I am trying to find a kill a program and all of its children in python (Linux), and so far all I have come up with is to run:

    os.system('/bin/ps -eo pgrp,pid,ppid,user,cmd > processlist.txt')

    Then read that in as a string and take the program name given at the command line, search through that string and send a signal to it. I really don't like this approach.. any other ideas?
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    os.system('/usr/bin/killall programname');
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    10
    Rep Power
    0
    Well yeah it's just that I have to manually do it.
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    I don't understand what you mean by manually? You can build the string and then execute it.
    Code:
    #!/usr/bin/env python
    import sys
    import os
    
    cmd = '/usr/bin/killall ' + sys.argv[1]
    os.execute(cmd)
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    10
    Rep Power
    0
    I meant manually as in, by taking the program name, finding its pid and all of its children, and sending the signals to those processes.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    133
    Rep Power
    12
    This is some code I wrote to make a Python version of "pidof" on Solaris:
    Code:
    #!/usr/bin/env python
    import os
    import sys
    import re
    
    if len(sys.argv) != 2:
        #print >> sys.stderr, "Provide one (and one only) pattern to match."
        sys.exit(1)
    
    ps = os.popen('ps -e')
    
    ps.readline() #-- To eliminate initial line
    for line in ps:
        try:
            match = re.search(r"%s" % (sys.argv[1],), line, re.IGNORECASE)
        except re.error:
            match = None
            sys.exit(1)
        if match != None:
            print re.search(r"^\s*?(\d+)(?=\s)", line).group(1),
    It's not exactly what you want, but that's the point.
    Anyway, it should give you an idea of what to do.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    14
    Well, I don't know of the easiest way to find the PID of the program you want to kill, but once you do, you should use os.kill to send the kill signal to it.
  14. #8
  15. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Well i don't know much on this subject, especially under *nix but if your trying to kill more than one (a group) process then i guess you should look at os.killpg().. as for getting the PID there are several interesting functions in the os module to do this

    my two cent,
    Mark.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo