getting a process's PID

Sebastian 'lunar' Wiesner basti.wiesner at gmx.net
Wed Dec 27 14:41:11 EST 2006


eldorado <eldorado at io.com> typed

> Hello,
> 
> I am trying to get python to give me the PID of a process (in this
> case
> HUB).  I have it working, except for the fact that the output includes
> \012 (newline).  Is there a way to ask python not to give me a
> newline?
> 
> Python 1.4 (Oct 14 1997) [C]
> Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>>> import os
>>>> g = os.popen("ps -ef | grep HUB | grep -v grep | awk '{ print $2
>>>> }'") h = g.readlines()
>>>> g.close()
>>>> h
> ['87334\012']
> 
> Thanks in advanced for any guidance.

Well, you could do everything in python itself, without using grep and
awk at all:

>>>> g = os.popen("ps -e -o pid,command")
>>>> for line in g.readlines():
>>>>     if 'HUB' in line:
>>>>         pid = line.strip().split(' ')[0]
>>>>         break
>>>> print pid

-- 
Freedom is always the freedom of dissenters.
                                      (Rosa Luxemburg)



More information about the Python-list mailing list