creating a daemon?

bmgx bmgx_no_sp at mgleesonprop.co.za
Thu Jan 8 11:42:35 EST 2004


Yeah ok, "FORK" being the golden word I failed to see at first, got some 
answers @ google groups but the most helpful was this link:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012

now to stick those DAEMONS with my fork()! anyone have an os.knife()?

Harry George wrote:
> bmgx <bmgx_no_sp at mgleesonprop.co.za> writes:
> 
> 
>>This is what I am trying to find out, instruction on how to create a
>>simple daemon on unix systems(Linux), can't find any info on usual
>>sources..
>>
> 
> 
> 1. The idea is to fork, clean up all the inheritable stuff like files
>    and environment vars, and then fork again.  The resulting process
>    can then be left running to do the daemon work.  I recall there is
>    a daemon.py module somewhere which does this all for you.  Here are
>    the basics:
> 
> def main(args):
>     #---first fork---
>     try:
>         pid = os.fork()
>         if pid > 0:
>             #---first parent---
>             sys.exit(0)
>     except OSError,e:
>         print >>sys.stderr, "fork #1 failed %d (%s)" % (e.errno,e.strerror)
>         sys.exit(1)
> 
>     
>     #---make a clean process---
>     os.chdir('/')
>     os.setsid()
>     os.umask(000)
>     child_in =open('/dev/null','r')
>     child_out=open('/dev/null','w')
>     os.dup2(child_in.fileno(), sys.stdin.fileno())
>     os.dup2(child_out.fileno(), sys.stdout.fileno())
>     os.dup2(child_out.fileno(), sys.stderr.fileno())
> 
>     #---second fork---
>     try:
>         pid = os.fork()
>         if pid > 0:
>             #---second parent---
>             sys.exit(0)
>     except OSError,e:
>         print >>sys.stderr, "fork #2 failed %d (%s)" % (e.errno,e.strerror)
>         sys.exit(1)
>     
> 
>     #---in clean child---
>     x=MyApp()
>     x.run()
> 
> 
> 2. To talk to that daemon, you can use a) files at known locations, b)
>    pipes (see popen), c) sockets with ad hoc protocols (see asyncchat
>    and SocketServer), d) predefined protocols (see Internet Protocols
>    and Services).  The various "server" modules have the setup code
>    from "1" above builtin, so you can ignore that level of detail.
>    ["see" references can be found in the normal python documentation's
>    Library Reference"].
> 
> 3. Once you have a working daemon, you need a way to start and stop
>    it.  You could manually start it each time you need it and then
>    "kill" it when done.  More commonly you would do it (in Linux and
>    UNIX) via a boot-time initialization script (typically found in
>    /etc/init.d).  Each *NIX has its own flavor of setting these up, so
>    you need to look at existing services and maybe copy-and-edit a
>    working script.  
> 
> 4. If security is an issue (it probably is), you may also want to hide
>    the service behind xinetd.  That is a whole setup story in its own
>    right.  And if security is really troublesome, you may need to
>    assure the python interpreter itself is safe.  That kind of
>    consideration is way out of my league.
> 




More information about the Python-list mailing list