How to launch an independant and detached process?
Olivier CARRERE
olivier at vibes.net
Mon Sep 18 12:33:28 EDT 2000
Hello,
I've been recently 'converted' to python and I've got a problem.
I wish to write a remote process manager wich can
start/stop/kill/query/info a bunch of processes.
I use Debian 2.2 GNU/Linux with python 1.5.2 with most library (Debian)
packages installed.
I've allready written and tested a multithreaded socket server wich can
handle all queries from clients, but I have difficulties to start
processes : I wish to start them completely detached from my manager but
they don't quite seem to :)
Let me explain how I proceed : The client connects to my manager, he gets
handled by a thread, he specifies the parameters of the controlled
process (exe name, exe params...), then he starts it. The start procedure
is handled by the run Detach functions :
def runDetach(exeName, exeParams):
"Runs a correctly detached process"
rd,wr=os.pipe() # to communicate son of son's pid...
if os.fork()==0:
detachPid=os.fork() if detachPid==0:
#setDaemon()
os.execv(exeName, exeParams)
else:
os.write(wr,str(detachPid)) os._exit(0) # this should\
potentially detach correctly the son process
pid=int(os.read(rd,5)) os.wait() os.close(rd) os.close(wr) return pid
The set Daemon function should be able to separate my process from its
process group...
def setDaemon(path="/"):
"""Set current executable as a deamon in the given path.
Horrible C-like function."""
# Close all file descriptors
i=0 while 1:
try:
print os.fstat(i)
except OSError:
break # Mmmmh, no more opened descriptor (maybe more after,\
but how can we know with this python release?)
os.close(i) i=i+1
# Set as no affiliated TTY...
myTTY=os.open("/dev/tty",os.O_RDWR) fcntl.ioctl(myTTY,IOCTL.TIOCNOTTY,\
0) os.close(myTTY)
# Change current path
os.chdir(path)
# Set the default group to none (independant)
os.setpgrp()
# Link the stdin, stdout, stderr to null
fd=os.open("/dev/null", os.O_RDWR) os.dup(fd) os.dup(fd)
Here are my problems :
- the setDeamon doesn't seem to work (my exe cannot be execved once it
has been launched).
- I can't close my client network socket after I launched runDetach() (my
client thread stays frozen into socket.close() ) !
- Is it the right way to detach a process ? (Maybe it's a more general
programmation issue)
Thanks all for your help,
- Olivier
More information about the Python-list
mailing list