Making a non-root daemon process

B

Ben Finney

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?
 
L

Leo Kislov

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

Ben Finney

Leo Kislov said:
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 :)
 
B

Ben Finney

Ben Finney said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top