Process Information by PID

Peter Hansen peter at engcorp.com
Thu Oct 16 09:43:38 EDT 2003


Thomas Guettler wrote:
> 
> Am Thu, 16 Oct 2003 08:56:18 -0400 schrieb Peter Hansen:
> 
> > Thomas Guettler wrote:
> >>
> >> I want to get some information about
> >> a process.
> >>
> >> I know its PID, and I need
> >> to know the commandline.
> >>
> >> I need something like:
> >> > ps -p $THISPID
> >
> > Surely you've already considered using popen?  On the other hand,
> > using "-p" doesn't give me the command line for processes, just
> > the name of the command itself.
> 
> You are right. In my case the name of the command would be enough.
> 
> popen: I read the PID from a file, the background process
> gets started before my script.

I think you misunderstood.  Use popen like this:

    outf = os.popen("ps -p %s" % PID)
    outputOfPsCommand = outf.read()

and then parse the contents of the outputOfPsCommand string...


> > What about reading from the /proc/$PID/cmdline pseudo-file ?
> 
> Is this portable to other unix systems?

Probably not.  You said "Linux", not multi-platform...

-Peter




More information about the Python-list mailing list