os.system() or os.execv() w/stdout redirect and PID tracking
Gordon McMillan
gmcm at hypernet.com
Thu May 6 15:00:22 EDT 1999
Michael Olivier writes:
> What's the best way from a python program to start another program
> in the background and: - redirect stdout/stderr to a file - get the
> process ID of the started program.
>
> It seems that:
>
> - os.system() call can start a program in the background, but
> doesn't return the pid (process ID) of the started program --
> returns 0
/snip/
> - if I call os.fork() and then os.execv(), the pid is trackable, but
> I don't know a way to redirect stdout/stderr
Something like this (snipped class method from working code):
def startChild(self):
c2pread, c2pwrite = os.pipe()
self.pid = os.fork()
if self.pid == 0: #Child
os.close(1) #close stdout
if os.dup(c2pwrite) != 1:
sys.stderr.write("bad write dup!\n")
try:
os.execv("/usr/local/bin/python",
("/usr/local/bin/python",
"receiver.py",
"%s_GATE"%self.thissite))
finally:
print "child could not start"
os._exit(1)
print "child pid =", self.pid
os.close(c2pwrite)
- Gordon
More information about the Python-list
mailing list