Connecting Tech Pros Worldwide Help | Site Map

Creating a daemon process in Python

 
LinkBack Thread Tools Search this Thread
  #1  
Old February 21st, 2007, 03:15 PM
Sakagami Hiroki
Guest
 
Posts: n/a
Default 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


  #2  
Old February 21st, 2007, 03:55 PM
Benjamin Niemann
Guest
 
Posts: n/a
Default Re: Creating a daemon process in Python

Hello,

Sakagami Hiroki wrote:
Quote:
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/
  #3  
Old February 21st, 2007, 03:55 PM
Eirikur Hallgrimsson
Guest
 
Posts: n/a
Default Re: Creating a daemon process in Python

Sakagami Hiroki wrote:
Quote:
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()


  #4  
Old February 21st, 2007, 09:15 PM
garrickp@gmail.com
Guest
 
Posts: n/a
Default Re: Creating a daemon process in Python

On Feb 21, 9:33 am, Eirikur Hallgrimsson <e...@mad.scientist.com>
wrote:
Quote:
Sakagami Hiroki wrote:
Quote:
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.

  #5  
Old February 21st, 2007, 09:45 PM
Benjamin Niemann
Guest
 
Posts: n/a
Default Re: Creating a daemon process in Python

garrickp@gmail.com wrote:
Quote:
On Feb 21, 9:33 am, Eirikur Hallgrimsson <e...@mad.scientist.com>
wrote:
Quote:
>Sakagami Hiroki wrote:
Quote:
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/
  #6  
Old February 21st, 2007, 10:15 PM
garrickp@gmail.com
Guest
 
Posts: n/a
Default Re: Creating a daemon process in Python

On Feb 21, 3:34 pm, Benjamin Niemann <p...@odahoda.dewrote:
Quote:
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

  #7  
Old February 22nd, 2007, 12:55 PM
okahashi@gmail.com
Guest
 
Posts: n/a
Default Re: Creating a daemon process in Python

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:
Quote:
Hello,
>
Sakagami Hiroki wrote:
Quote:
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/

  #8  
Old February 22nd, 2007, 02:15 PM
Grant Edwards
Guest
 
Posts: n/a
Default Re: Creating a daemon process in Python

On 2007-02-22, okahashi@gmail.com <okahashi@gmail.comwrote:
Quote:
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??
  #9  
Old February 22nd, 2007, 07:35 PM
Joshua J. Kugler
Guest
 
Posts: n/a
Default Re: Creating a daemon process in Python

Benjamin Niemann wrote:
Quote:
Quote:
>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

  #10  
Old February 23rd, 2007, 01:45 PM
Nick Craig-Wood
Guest
 
Posts: n/a
Default Re: Creating a daemon process in Python

Eirikur Hallgrimsson <eh@mad.scientist.comwrote:
Quote:
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)

Quote:
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?
Quote:
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 <nick@craig-wood.com-- http://www.craig-wood.com/nick
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.