getpid Usage

Donn Cave donn at u.washington.edu
Fri Jul 14 12:11:15 EDT 2000


In article <8kksms+n5h5 at eGroups.com>, david at tumbleweed.com wrote:
| Is there some convenient way of getting os.getpid to return the PID of
| a given executable? What I want to do is start a process and know it's
| PID. When I do the following I get the PID of the python executable.
| 
| os.system("path")
| os.getpid()

Give me a break, in that example the "path" process should have exited
when control returns from the system() function, so there is no PID
left to report.  So the first question we have to ask is, what are
you really doing here?  Do you append an & to the command to get the
shell to run it in the background and exit?  Or does your particular
command do this on its own?

In the first case, there are a couple of things you can do.  You can
exec the command directly as the shell would do, rather than using
system() to invoke a shell.  The popen2 module does that, for an
example, and you can ignore the pipe, dup and close stuff there.
The parent fork will get the child's PID value from fork().  The
other option would be something like os.popen('cmd &\necho $!', 'r').
You will get back the PID as a text string in the pipe.

In the second case, there's nothing you can do without control over
the application, since that's where the fork you're interested in occurs.
As already suggested in another followup, in such a case the application
ought to write out its PID to a file, in the standard location for your
platform - /var/run, /etc, whatever.

	Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list