473,406 Members | 2,312 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,406 software developers and data experts.

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 9114
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.activestate.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

"""Facilities 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.scientist.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.Thread(target=im_a_thread)
thread.setDaemon(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.scientist.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.Thread(target=im_a_thread)
thread.setDaemon(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.dewrote:
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.dewrote:
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.activestate.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.activestate.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.scientist.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.devnull, os.O_RDWR)
os.dup2(null, sys.stdin.fileno())
os.dup2(null, sys.stdout.fileno())
os.dup2(null, sys.stderr.fileno())
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
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,...
3
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
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...
1
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...
1
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...
3
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...
3
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....
3
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"...
6
by: Johny | last post by:
Is it possible to run a Python program as daemon? Thanks
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
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...

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.