Creating a daemon process in Python

S

Sakagami Hiroki

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,
 
E

Eirikur Hallgrimsson

Sakagami said:
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()
 
G

garrickp

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

Benjamin Niemann

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).
 
G

garrickp

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
 
O

okahashi

Thanks all,

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

Grant Edwards

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. ;)
 
N

Nick Craig-Wood

Eirikur Hallgrimsson said:
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?
 

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,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top