the dreaded double fork

J

John Hunter

I am using python to run an xml rpc daemon using SimpleXMLRPCServer.
Following a cookbook recipe, I am trying to do a double fork so that
when I exit the shell the daemon remains running. But it doesn't.
Here is how I start the script
ssh somehost
sudo python mydaemon start&

But when I exit the shell, I lose the process.

What am I doing wrong? Here is my script, with all the non server
stuff snipped.

import sys, os


pidfile = '/tmp/ogtt_daemon.pid'
host = 'dardd.bsd.uchicago.edu'
port = 2358


class AnalysisServer:
snip

def main():
import SimpleXMLRPCServer

while 1:
# Daemon's main code goes here
server = SimpleXMLRPCServer.SimpleXMLRPCServer((host, port))
server.register_instance(AnalysisServer())
server.serve_forever()


def get_daemon_pid():
if os.path.exists(pidfile):
pid = open(pidfile, 'r').read()
return int(pid)

else:
return None

def stopd():
print 'Stopping daemon...',
if not os.path.exists(pidfile):
print 'Daemon does not appear to be running'
return
pid = get_daemon_pid()
os.popen('kill -9 %d' % pid)
print 'OK'
os.remove(pidfile)

if __name__ == "__main__":

if sys.argv[1]=='start' and os.path.exists(pidfile):
print 'Process appears to be running'
sys.exit()

if sys.argv[1]=='stop':
stopd()
sys.exit()

# do the UNIX double-fork magic, see Stevens' "Advanced
# Programming in the UNIX Environment" for details (ISBN 0201563177)
try:
pid = os.fork()
if pid > 0:
# exit first parent
sys.exit(0)
except OSError, e:
print >>sys.stderr, "fork #1 failed: %d (%s)" % (e.errno, e.strerror)
sys.exit(1)

# decouple from parent environment
os.chdir("/")
os.setsid()
os.umask(0)

# do second fork
try:
pid = os.fork()
if pid > 0:
# exit from second parent, print eventual PID before
print "Daemon PID %d" % pid
file(pidfile, 'w').write('%d\n'%pid)
sys.exit(0)
except OSError, e:
print >>sys.stderr, "fork #2 failed: %d (%s)" % (e.errno, e.strerror)
sys.exit(1)

# start the daemon main loop
main()
 
Y

Yermat

John said:
I am using python to run an xml rpc daemon using SimpleXMLRPCServer.
Following a cookbook recipe, I am trying to do a double fork so that
when I exit the shell the daemon remains running. But it doesn't.
Here is how I start the script




But when I exit the shell, I lose the process.

What am I doing wrong? Here is my script, with all the non server
stuff snipped.


You will find here one(?) good way of doing it :
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/278731
 
I

Ivan Voras

John said:
I am using python to run an xml rpc daemon using SimpleXMLRPCServer.
Following a cookbook recipe, I am trying to do a double fork so that
when I exit the shell the daemon remains running. But it doesn't.
Here is how I start the script

try:
nohup sudo python mydaemon start &
 
G

Guest

I am using python to run an xml rpc daemon using SimpleXMLRPCServer.
Following a cookbook recipe, I am trying to do a double fork so that
when I exit the shell the daemon remains running. But it doesn't.
Here is how I start the script


But when I exit the shell, I lose the process.

You need to close the file descriptors to the controlling TTY. This is how I
do it.

    f = file('/dev/null', 'r+')
    fd = f.fileno()
    os.dup2(fd,0)
    os.dup2(fd,1)
    os.dup2(fd,2)
    f.close()

Beware, sys.stdxxx.close() is not enough.
 
D

Doug Holton

John said:
I am using python to run an xml rpc daemon using SimpleXMLRPCServer.
Following a cookbook recipe, I am trying to do a double fork so that
when I exit the shell the daemon remains running. But it doesn't.
Here is how I start the script

See also http://www.noah.org/python/daemonize.py

But another way to do it without needing nohup or daemonize is to double
background it and redirect stdout and stderr:

(python mydaemon start > /dev/null 2>&1 & ) &

Then you can exit and it will still be running (check ps -A to see it).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top