Programmatically Getting Process ID

Moshe Zadka moshez at math.huji.ac.il
Sat Mar 4 05:00:22 EST 2000


On Fri, 3 Mar 2000, Hernan M.Foffani wrote:

> The portable way to do it (in UN*X, I mean) is to run a script and get
> the output (via pipes), something like:
> 
> #!/bin/sh
> ps -ef | grep "$1" | grep -v grep | awk '{ print $1 }'

And in 100% Pure Python(tm):

#!/usr/bin/env python
import os, string

lines = os.popen("ps -ef").readlines()
processes = []

for line in line:
	if string.find(line, sys.argv[1]) != -1:
		processes.append(string.atoi(string.split(line)[0]))

for process in processes:
	os.kill(process)

--
Moshe Zadka <mzadka at geocities.com>. 
http://www.oreilly.com/news/prescod_0300.html





More information about the Python-list mailing list