473,769 Members | 6,248 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Mar 23 '07 #1
3 6194
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.

Mar 23 '07 #2
"Leo Kislov" <Le********@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

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
2200
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. Php script can load my library successfully. And Now I port my library to HP(HP-UX 11.0) and IBM(AIX 4.1.3) system.
3
9172
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 RegExp object to be non-greedy as using the ? quantifier doesn't seem to work. --------- SAMPLE ----------------
7
4018
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 do a database query and what I wind up with is a structure Q of arrays: Q.f1 is field f1 in row n, Q.f1.length == Q.f2.length == Q.f3.length is the record count of the query. (This representation is of course backwards, but that's what...
19
56697
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 least, SHOULD make the computer speaker beep. I know that whether the computer speaker actually beeps or not is largely dependent on the OS I'm using. But I'm not so much concerned about that (at this stage). I just want to know if ANSI C++...
34
3706
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 thinking of making all these pimpl class declarations public. Reasoning is that since only the code within the ..cc file will need to ever access them, why protect them in ways that would make access to them more difficult and obfuscated? What...
351
13123
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 do) occur in any language. These assumptions are generally made because of familiarity with the language. As a non-code example, consider the idea that the faulty code is written by blackguards bent on foulling the language. The term...
84
15899
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
3379
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
4512
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): //------------------------------------------------------------------ template<typename Data_t> class SmartPointer { Data_t* data; void(*deleterFunc)(Data_t*);
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10214
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10048
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9996
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5304
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3963
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.