creating a daemon?

Discussion in 'Python' started by Harry George, Jan 8, 2004.

  1. Harry George

    Harry George Guest

    bmgx <> 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.

    --

    6-6M31 Knowledge Management
    Phone: (425) 342-5601
     
    Harry George, Jan 8, 2004
    #1
    1. Advertisements

  2. Harry George

    bmgx Guest

    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..
     
    bmgx, Jan 8, 2004
    #2
    1. Advertisements

  3. bmgx wrote:

    > 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..


    google: daemonize python fork

    Works perfect for me.

    Diez
     
    Diez B. Roggisch, Jan 8, 2004
    #3
  4. Harry George

    bmgx Guest

    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 <> 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.
    >
     
    bmgx, Jan 8, 2004
    #4
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt Stevens

    Forking HTTP Daemon - Problem

    Matt Stevens, Dec 8, 2003, in forum: Perl
    Replies:
    1
    Views:
    826
    Jim Gibson
    Dec 9, 2003
  2. David Pratt
    Replies:
    1
    Views:
    484
    McBooCzech
    Sep 15, 2005
  3. Jon Monteleone
    Replies:
    1
    Views:
    918
    Diez B. Roggisch
    Sep 25, 2005
  4. Sakagami Hiroki

    Creating a daemon process in Python

    Sakagami Hiroki, Feb 21, 2007, in forum: Python
    Replies:
    9
    Views:
    1,280
    Nick Craig-Wood
    Feb 23, 2007
  5. kernel.lover

    Creating socket programs as daemon

    kernel.lover, Mar 3, 2005, in forum: C Programming
    Replies:
    2
    Views:
    422
    Jack Klein
    Mar 3, 2005
  6. Floris Bruynooghe
    Replies:
    1
    Views:
    617
    Floris Bruynooghe
    Mar 24, 2009
  7. vineet daniel

    creating python daemon ?

    vineet daniel, Sep 21, 2010, in forum: Python
    Replies:
    6
    Views:
    1,245
    vineet daniel
    Sep 26, 2010
  8. Daemon Win32::Daemon;

    , Sep 7, 2006, in forum: Perl Misc
    Replies:
    0
    Views:
    426
Loading...