faqts : Computers : Programming : Languages : Python : Snippets

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

22 of 35 people (63%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

Getting process ID

Jul 1st, 2008 11:29
sheana krat, Nathan Wallace, Hans Nowak, Snippet 382, Moshe Zadka


"""
Packages: operating_systems.unix
"""
"""
> 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)
http://www.indianhandicraftsexporters.com
http://www.topgiftsideas.com
http://www.eindianweddingcards.com