|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
|
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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 Puzzle of the Month solved by sizeablegrin, etienne141 and L7Sqr, superior C/C++ programmers of the month |
|
#3
|
|||
|
|||
|
Well yeah it's just that I have to manually do it.
|
|
#4
|
||||
|
||||
|
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) |
|
#5
|
|||
|
|||
|
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.
|
|
#6
|
|||
|
|||
|
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. |
|
#7
|
|||
|
|||
|
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.
|
|
#8
|
||||
|
||||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Find and kill a program |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|