473,395 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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.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
3 6172
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Simon | last post by:
Hello. I wanna use my dynamic shared library(built with C language) with php script. (PHP version is 4.0.6) So I making a shared library(*.so) in Solaris environment(2.7) and it is successful....
3
by: Jane Doe | last post by:
Hello, I need to browse a list of hyperlinks, each followed by an author, and remove the links only for certain authors. 1. I searched the archives on Google, but didn't find how to tell the...
7
by: Christopher Jeris | last post by:
I am relatively new to JavaScript, though not to programming, and I'm having trouble finding the idiomatic JS solution to the following problem. I have a table with (say) fields f1, f2, f3. I...
19
by: Anon Email | last post by:
Hi everyone, Let's see, now. This question is about the capabilities of ANSI C++. I want to write and compile code in ANSI C++ that, when compiled, will make the computer speaker beep; or, at...
34
by: Asfand Yar Qazi | last post by:
Hi, I'm creating a library where several classes are intertwined rather tightly. I'm thinking of making them all use pimpls, so that these circular dependancies can be avoided easily, and I'm...
351
by: CBFalconer | last post by:
We often find hidden, and totally unnecessary, assumptions being made in code. The following leans heavily on one particular example, which happens to be in C. However similar things can (and...
84
by: Peter Olcott | last post by:
Is there anyway of doing this besides making my own string from scratch? union AnyType { std::string String; double Number; };
4
by: abendstund | last post by:
Hi, I have the following code and trouble with ambiguity due to operator overloading.. The code is also at http://paste.nn-d.de/441 snip>>
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.