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

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

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

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. David Pratt
    Replies:
    1
    Views:
    409
    McBooCzech
    Sep 15, 2005
  2. Replies:
    0
    Views:
    689
  3. Jean-Paul Calderone
    Replies:
    0
    Views:
    480
    Jean-Paul Calderone
    Mar 20, 2009
  4. Floris Bruynooghe
    Replies:
    1
    Views:
    472
    Floris Bruynooghe
    Mar 24, 2009
  5. Daemon Win32::Daemon;

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

Share This Page