Making a non-root daemon process

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ben Finney

    #1

    Making a non-root daemon process

    Howdy all,

    For making a Python program calve off an independent daemon process of
    itself, I found Carl J. Schroeder's recipe in the ASPN Python Cookbook.
    <URL:http://aspn.activestat e.com/ASPN/Cookbook/Python/Recipe/278731>

    This is a thorough approach, and I'm cribbing a simpler process from
    this example. One thing that strikes me is that the algorithm seems to
    depend on running the program as the root user.

    import os

    def become_daemon() :
    pid = os.fork()
    if pid == 0:
    # This is the child of the fork

    # Become a process leader of a new process group
    os.setsid()

    # Fork again and exit this parent
    pid = os.fork()
    if pid == 0:
    # This is the child of the second fork -- the running process.
    pass
    else:
    # This is the parent of the second fork
    # Exit to prevent zombie process
    os._exit(0)
    else:
    # This is the parent of the fork
    os._exit(0)

    become_daemon()
    # Continue with the program


    The double-fork seems to be to:
    - Allow the first forked child to start a new process group
    - Allow the second forked child to be orphaned immediately

    The problem I'm having is that 'os.setsid()' fails with 'OSError:
    [Errno 1] Operation not permitted' unless I run the program as the
    root user. This isn't a program that I want necessarily running as
    root.

    What does the 'os.setsid()' gain me? How can I get that without being
    the root user?

    --
    \ "I went to a general store. They wouldn't let me buy anything |
    `\ specifically." -- Steven Wright |
    _o__) |
    Ben Finney

  • Leo Kislov

    #2
    Re: Making a non-root daemon process

    On Mar 22, 11:19 pm, Ben Finney <b...@benfinney .id.auwrote:
    Howdy all,
    >
    For making a Python program calve off an independent daemon process of
    itself, I found Carl J. Schroeder's recipe in the ASPN Python Cookbook.
    <URL:http://aspn.activestat e.com/ASPN/Cookbook/Python/Recipe/278731>
    >
    This is a thorough approach, and I'm cribbing a simpler process from
    this example. One thing that strikes me is that the algorithm seems to
    depend on running the program as the root user.
    >
    import os
    >
    def become_daemon() :
    pid = os.fork()
    if pid == 0:
    # This is the child of the fork
    >
    # Become a process leader of a new process group
    os.setsid()
    >
    # Fork again and exit this parent
    pid = os.fork()
    if pid == 0:
    # This is the child of the second fork -- the running process.
    pass
    else:
    # This is the parent of the second fork
    # Exit to prevent zombie process
    os._exit(0)
    else:
    # This is the parent of the fork
    os._exit(0)
    >
    become_daemon()
    # Continue with the program
    >
    The double-fork seems to be to:
    - Allow the first forked child to start a new process group
    - Allow the second forked child to be orphaned immediately
    >
    The problem I'm having is that 'os.setsid()' fails with 'OSError:
    [Errno 1] Operation not permitted' unless I run the program as the
    root user. This isn't a program that I want necessarily running as
    root.
    It works for me. I mean your program above produces no exceptions for
    me on Debian 3.1 python2.4
    What does the 'os.setsid()' gain me?
    It dettaches you from terminal. It means you won't receive signals
    from terminal for sure. Like SIGINT and SIGHUP, but there are maybe
    other.
    How can I get that without being
    the root user?
    Maybe you can go over the list of all possible signals from the
    terminal and notify kernel that you want to ignore them. Sounds
    similar to dettaching from the terminal, but maybe there some
    differences. But the fact that os.setsid fails for you is weird
    anyway.

    -- Leo.

    Comment

    • Ben Finney

      #3
      Re: Making a non-root daemon process

      "Leo Kislov" <Leo.Kislov@gma il.comwrites:
      On Mar 22, 11:19 pm, Ben Finney <b...@benfinney .id.auwrote:
      The problem I'm having is that 'os.setsid()' fails with 'OSError:
      [Errno 1] Operation not permitted' unless I run the program as the
      root user. This isn't a program that I want necessarily running as
      root.
      >
      It works for me. I mean your program above produces no exceptions
      for me on Debian 3.1 python2.4
      Hmm. I typed the example program in as a simplified version of what
      I'm doing; but didn't actually *run* it. When I do run it, I get no
      exception, as you say.

      Now I'll have to find out what significant difference there is between
      my failing code and this example, and report back in this thread.

      Thanks for showing me this far :-)

      --
      \ "Some people, when confronted with a problem, think 'I know, |
      `\ I'll use regular expressions'. Now they have two problems." -- |
      _o__) Jamie Zawinski, in alt.religion.em acs |
      Ben Finney

      Comment

      • Ben Finney

        #4
        Re: Making a non-root daemon process

        Ben Finney <bignose+hate s-spam@benfinney. id.auwrites:
        Hmm. I typed the example program in as a simplified version of what
        I'm doing; but didn't actually *run* it. When I do run it, I get no
        exception, as you say.
        >
        Now I'll have to find out what significant difference there is
        between my failing code and this example, and report back in this
        thread.
        It turns out that, in re-typing the algorithm in the newsgroup
        message, I got all the branches correct; but in the code that wasn't
        working, I had reversed one of them :-)

        All fine now. We return you to your regularly scheduled programming.

        --
        \ "My aunt gave me a walkie-talkie for my birthday. She says if |
        `\ I'm good, she'll give me the other one next year." -- Steven |
        _o__) Wright |
        Ben Finney

        Comment

        Working...