473,647 Members | 3,370 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating a daemon process in Python

Hi,

What is the easiest way to create a daemon process in Python? Google
says I should call fork() and other system calls manually, but is
there no os.daemon() and the like?

Regards,

--
Sakagami Hiroki

Feb 21 '07 #1
9 9141
Hello,

Sakagami Hiroki wrote:
What is the easiest way to create a daemon process in Python? Google
says I should call fork() and other system calls manually, but is
there no os.daemon() and the like?
You could try
<http://aspn.activestat e.com/ASPN/Cookbook/Python/Recipe/278731>
HTH

--
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/
Feb 21 '07 #2
Sakagami Hiroki wrote:
Hi,

What is the easiest way to create a daemon process in Python?
I find that this works great. I just pasted my copy, I think you can
find it via Google.

Eirikur
# Daemon Module - basic facilities for becoming a daemon process
# By Coy Krill
# Combines ideas from Steinar Knutsens daemonize.py and
# Jeff Kunces demonize.py

"""Faciliti es for Creating Python Daemons"""

import os
import time
import sys

class NullDevice:
def write(self, s):
pass

def daemonize():
if (not os.fork()):
# get our own session and fixup std[in,out,err]
os.setsid()
sys.stdin.close ()
sys.stdout = NullDevice()
sys.stderr = NullDevice()
if (not os.fork()):
# hang around till adopted by init
ppid = os.getppid()
while (ppid != 1):
time.sleep(0.5)
ppid = os.getppid()
else:
# time for child to die
os._exit(0)
else:
# wait for child to die and then bail
os.wait()
sys.exit()
Feb 21 '07 #3
On Feb 21, 9:33 am, Eirikur Hallgrimsson <e...@mad.scien tist.com>
wrote:
Sakagami Hiroki wrote:
What is the easiest way to create a daemon process in Python?
I've found it even easier to use the built in threading modules:

import time

t1 = time.time()
print "t_poc.py called at", t1

import threading

def im_a_thread():
time.sleep(10)
print "This is your thread speaking at", time.time()

thread = threading.Threa d(target=im_a_t hread)
thread.setDaemo n(True)
thread.start()
t2 = time.time()
print "Time elapsed in main thread:", t2 - t1
Of course, your mileage may vary.

Feb 21 '07 #4
ga******@gmail. com wrote:
On Feb 21, 9:33 am, Eirikur Hallgrimsson <e...@mad.scien tist.com>
wrote:
>Sakagami Hiroki wrote:
What is the easiest way to create a daemon process in Python?

I've found it even easier to use the built in threading modules:

import time

t1 = time.time()
print "t_poc.py called at", t1

import threading

def im_a_thread():
time.sleep(10)
print "This is your thread speaking at", time.time()

thread = threading.Threa d(target=im_a_t hread)
thread.setDaemo n(True)
thread.start()
t2 = time.time()
print "Time elapsed in main thread:", t2 - t1
Of course, your mileage may vary.
That's not a daemon process (which are used to execute 'background services'
in UNIX environments).

--
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/
Feb 21 '07 #5
On Feb 21, 3:34 pm, Benjamin Niemann <p...@odahoda.d ewrote:
That's not a daemon process (which are used to execute 'background services'
in UNIX environments).
I had not tested this by running the script directly, and in writing a
response, I found out that the entire interpreter closed when the main
thread exited (killing the daemonic thread in the process). This is
different behavior from running the script interactively, and thus my
confusion.

Thanks! ~Garrick

Feb 21 '07 #6
Thanks all,

I understood there is no shortcut function like BSD daemon(). I'll do
it manually using examples from cookbook...

On 2$B7n(B22$BF |(B, $B8aA0(B1:41, Benjamin Niemann <p...@odahoda.d ewrote:
Hello,

Sakagami Hiroki wrote:
What is the easiest way to create a daemon process in Python? Google
says I should call fork() and other system calls manually, but is
there no os.daemon() and the like?

You could try
<http://aspn.activestat e.com/ASPN/Cookbook/Python/Recipe/278731>

HTH

--
Benjamin Niemann
Email: pink at odahoda dot de
WWW:http://pink.odahoda.de/

Feb 22 '07 #7
On 2007-02-22, ok******@gmail. com <ok******@gmail .comwrote:
I understood there is no shortcut function like BSD daemon(). I'll do
it manually using examples from cookbook...
Sure would be nice if somebody posted one. ;)

--
Grant Edwards grante Yow! Oh, I get it!! "The
at BEACH goes on", huh,
visi.com SONNY??
Feb 22 '07 #8
Benjamin Niemann wrote:
>What is the easiest way to create a daemon process in Python? Google
says I should call fork() and other system calls manually, but is
there no os.daemon() and the like?
You could try
<http://aspn.activestat e.com/ASPN/Cookbook/Python/Recipe/278731>
Also, more discussion on the topic here:
http://aspn.activestate.com/ASPN/Coo...n/Recipe/66012

j

--
Joshua Kugler
Lead System Admin -- Senior Programmer
http://www.eeinternet.com
PGP Key: http://pgp.mit.edu/ Â*ID 0xDB26D7CE

--
Posted via a free Usenet account from http://www.teranews.com

Feb 22 '07 #9
Eirikur Hallgrimsson <eh@mad.scienti st.comwrote:
def daemonize():
if (not os.fork()):
# get our own session and fixup std[in,out,err]
os.setsid()
sys.stdin.close ()
sys.stdout = NullDevice()
sys.stderr = NullDevice()
That doesn't close the underlying file descriptors...

Here is another method which does :-

null = os.open(os.devn ull, os.O_RDWR)
os.dup2(null, sys.stdin.filen o())
os.dup2(null, sys.stdout.file no())
os.dup2(null, sys.stderr.file no())
os.close(null)

if (not os.fork()):
# hang around till adopted by init
ppid = os.getppid()
while (ppid != 1):
time.sleep(0.5)
ppid = os.getppid()
Why do you need hang around until adopted by init? I've never see
that in a daemonize recipe before?
else:
# time for child to die
os._exit(0)
else:
# wait for child to die and then bail
os.wait()
sys.exit()
--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Feb 23 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
5651
by: Rob Hunter | last post by:
Hi all. I want to run a Python daemon that manages a "to-do" queue. I want it so that the daemon is always running, where its running consists of looking at the queue to see if it has any jobs, and if it does, remove the job from the queue and "do the job". This all seems quite doable using the synchronized Queue.py module and this page (http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012) which
3
2511
by: bmgx | last post by:
This is what I am trying to find out, instruction on how to create a simple daemon on unix systems(Linux), can't find any info on usual sources..
1
1553
by: Valia | last post by:
hello all, here is something i would like to do, i think this can be done with a daemon or something like named pipe or even a daemon waiting its input from named pipe? do not know. please can you help me and tell is this a good way to do it ? and also maybeme some starting points (it is still too confused for me to google anything) or simple examples.
1
2138
by: Bob Swerdlow | last post by:
I've created a Python daemon that starts a bunch of BitTorrent downloader process. Everything is working fine when I start the daemon by hand (logged on as root). I can quit the session and see that the daemon is running and the subprocesses are running, too. However, when I configure the system to automatically start the daemon on boot-up, the daemon runs, but the processes are not created. This is on Solaris 8 with Python 2.3.3. ...
1
3552
by: David Pratt | last post by:
Hi. I am running a zope server. Zope runs 4 threads and I have a document processing method that can require minutes to run so I do not want to run out of threads. A solution to this is to run this process asynchronously. What I am hoping to do is send a signal to a python deamon to run a process and then go back to sleep. I am hoping to find a clear example of: * sending a signal from a python script to start a python daemon...
3
5207
by: Thomas Dybdahl Ahle | last post by:
Hi, I'm writing a program, using popen4(gnuchess), The problem is, that gnuchess keeps running after program exit. I know about the atexit module, but in java, you could make a process a daemon process, and it would only run as long as the real processes ran. I think this is a better way to stop gnuchess, as you are 100% sure, that it'll stop. Can you do this with popen?
3
6188
by: Ben Finney | last post by:
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.
3
4279
by: paul | last post by:
I am writing a daemon process that reads data from the serial port / dev/ttyS0. I am using pyserial & the method for setting up a daemon described in "Chris' Python Page" (http://homepage.hispeed.ch/py430/ python/) on an Ubuntu linux pc. Everything works great EXCEPT... in the daemon script, there are two lines to change the uid & gid that the script runs as: os.setegid(10)
6
3983
by: Johny | last post by:
Is it possible to run a Python program as daemon? Thanks
0
8210
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8715
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
8645
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
8371
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
7209
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...
1
6126
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2633
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
1
1818
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1516
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.