[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Do any one know how to make a daemon in python



Salam, I tried a similar daemon and it was still working after closing the terminal:
 

#!/usr/bin/python

import os

def lo():
 while 1: print 1
p=os.fork()
if (p==0):
 lo()
else:
 print "error:", p

and when trying yours, it also behaves properly as a deamon :)

I don't why this is happening with you, but maybe... (I'll think over it again)

N.B. The other file that I gave the link in previous email is attached.

On 12/24/07, moayyad sadi <alsadi at gmail dot com> wrote:
> In Python, you can use os.fork() and os.setsid() which are the substitutes
> of C's fork() and setsid() (of course, you need to import os library).
>
>
I tried that
it did not work,
I write a small script that do
from os import *
from time import * # or something like that
def loop():
for i in xrange(65000): print i; sleep(i)
#main
p=fork()
if p==0: loop()
else: print "this is the parent, child pid=%d\ndone\n" % p

and when I run it then close the terminal then open another one and type
ps ax | grep python
it did not appear

while the same code in C works

> http://homepage.hispeed.ch/py430/python/daemon.py
>
please send it to my email as an attachement, I don't have internet
please,
_______________________________________________
Developer mailing list
Developer at arabeyes dot org
http://lists.arabeyes.org/mailman/listinfo/developer



--
It is the darkest under the lighthouse
#!/usr/bin/env python

###########################################################################
# configure these paths:
LOGFILE = '/var/log/pydaemon.log'
PIDFILE = '/var/run/pydaemon.pid'

# and let USERPROG be the main function of your project
import mymain
USERPROG = mymain.main
###########################################################################

#based on Jürgen Hermanns http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012
import sys, os

class Log:
    """file like for writes with auto flush after each write
    to ensure that everything is logged, even during an
    unexpected exit."""
    def __init__(self, f):
        self.f = f
    def write(self, s):
        self.f.write(s)
        self.f.flush()

def main():
    #change to data directory if needed
    os.chdir("/root/data")
    #redirect outputs to a logfile
    sys.stdout = sys.stderr = Log(open(LOGFILE, 'a+'))
    #ensure the that the daemon runs a normal user
    os.setegid(103)     #set group first "pydaemon"
    os.seteuid(103)     #set user "pydaemon"
    #start the user program here:
    USERPROG()

if __name__ == "__main__":
    # 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("/")   #don't prevent unmounting....
    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
            open(PIDFILE,'w').write("%d"%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()