Getting a proccesses ID

Ivica StRanGy at
Fri Sep 6 12:52:49 EDT 2002

On Fri, 06 Sep 2002 15:05:20 GMT, Micah Mayo <astrophels at> wrote:
> Hi,
> I'm writing a little tool for SysAdmins on FreeBSD - I'm trying to make
> it as portable as possible(as far as the unices go), and have come upon
> a problem - one of the things this tool does is restarts various deamons
> on the system, in order to do that I have to send a killsignal to the
> pid. The problem is getting the process id - right now I've got a hack
> using ps, grep, and awk, which I doubt will translate well to other
> flavors of *nix because they all seem to have their own method of
> delivering info via ps. Can anyone tell me a cross platform way of
> grabbing a particular processes ID
> Here's what I'm doing now, by the way:
> pid = os.popen("ps -x | grep sshd | grep -v grep | awk '{ print $1 }'")
> pid = pid.atoi()

Maybe this will help you:

def GetPids(list, procname):
        pidlist = []
        for i in range(len(list)):
                if string.find(list[i], procname) != -1:
                        str = list[i].strip()
                        sp = string.find(str, ' ')
                        pid = str[:sp].strip()
        mypid = os.getpid()
        if mypid in pidlist:
        return pidlist
This I use for killing a proccess ny his name and not killing myself if the
process is python

the list argument is a list os proccesses which i got using ...

pidlist = os.popen('ps').read()
pidlist = pidlist.split('\n')

# RealName: Ivica Munitic
# E-Mail: imunitic at

More information about the Python-list mailing list