By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,276 Members | 2,137 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,276 IT Pros & Developers. It's quick & easy.

Making a non-root daemon process

P: n/a
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.activestate.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

Mar 23 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.activestate.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.

Mar 23 '07 #2

P: n/a
"Leo Kislov" <Le********@gmail.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.emacs |
Ben Finney

Mar 23 '07 #3

P: n/a
Ben Finney <bi****************@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

Mar 24 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.