473,382 Members | 1,775 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,382 software developers and data experts.

creating a daemon?

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

Jul 18 '05 #1
3 2495
bmgx wrote:
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..


google: daemonize python fork

Works perfect for me.

Diez
Jul 18 '05 #2
bmgx <bm********@mgleesonprop.co.za> writes:
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. The idea is to fork, clean up all the inheritable stuff like files
and environment vars, and then fork again. The resulting process
can then be left running to do the daemon work. I recall there is
a daemon.py module somewhere which does this all for you. Here are
the basics:

def main(args):
#---first fork---
try:
pid = os.fork()
if pid > 0:
#---first parent---
sys.exit(0)
except OSError,e:
print >>sys.stderr, "fork #1 failed %d (%s)" % (e.errno,e.strerror)
sys.exit(1)
#---make a clean process---
os.chdir('/')
os.setsid()
os.umask(000)
child_in =open('/dev/null','r')
child_out=open('/dev/null','w')
os.dup2(child_in.fileno(), sys.stdin.fileno())
os.dup2(child_out.fileno(), sys.stdout.fileno())
os.dup2(child_out.fileno(), sys.stderr.fileno())

#---second fork---
try:
pid = os.fork()
if pid > 0:
#---second parent---
sys.exit(0)
except OSError,e:
print >>sys.stderr, "fork #2 failed %d (%s)" % (e.errno,e.strerror)
sys.exit(1)
#---in clean child---
x=MyApp()
x.run()
2. To talk to that daemon, you can use a) files at known locations, b)
pipes (see popen), c) sockets with ad hoc protocols (see asyncchat
and SocketServer), d) predefined protocols (see Internet Protocols
and Services). The various "server" modules have the setup code
from "1" above builtin, so you can ignore that level of detail.
["see" references can be found in the normal python documentation's
Library Reference"].

3. Once you have a working daemon, you need a way to start and stop
it. You could manually start it each time you need it and then
"kill" it when done. More commonly you would do it (in Linux and
UNIX) via a boot-time initialization script (typically found in
/etc/init.d). Each *NIX has its own flavor of setting these up, so
you need to look at existing services and maybe copy-and-edit a
working script.

4. If security is an issue (it probably is), you may also want to hide
the service behind xinetd. That is a whole setup story in its own
right. And if security is really troublesome, you may need to
assure the python interpreter itself is safe. That kind of
consideration is way out of my league.

--
ha************@boeing.com
6-6M31 Knowledge Management
Phone: (425) 342-5601
Jul 18 '05 #3
Yeah ok, "FORK" being the golden word I failed to see at first, got some
answers @ google groups but the most helpful was this link:

http://aspn.activestate.com/ASPN/Coo...n/Recipe/66012

now to stick those DAEMONS with my fork()! anyone have an os.knife()?

Harry George wrote:
bmgx <bm********@mgleesonprop.co.za> writes:

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. The idea is to fork, clean up all the inheritable stuff like files
and environment vars, and then fork again. The resulting process
can then be left running to do the daemon work. I recall there is
a daemon.py module somewhere which does this all for you. Here are
the basics:

def main(args):
#---first fork---
try:
pid = os.fork()
if pid > 0:
#---first parent---
sys.exit(0)
except OSError,e:
print >>sys.stderr, "fork #1 failed %d (%s)" % (e.errno,e.strerror)
sys.exit(1)
#---make a clean process---
os.chdir('/')
os.setsid()
os.umask(000)
child_in =open('/dev/null','r')
child_out=open('/dev/null','w')
os.dup2(child_in.fileno(), sys.stdin.fileno())
os.dup2(child_out.fileno(), sys.stdout.fileno())
os.dup2(child_out.fileno(), sys.stderr.fileno())

#---second fork---
try:
pid = os.fork()
if pid > 0:
#---second parent---
sys.exit(0)
except OSError,e:
print >>sys.stderr, "fork #2 failed %d (%s)" % (e.errno,e.strerror)
sys.exit(1)
#---in clean child---
x=MyApp()
x.run()
2. To talk to that daemon, you can use a) files at known locations, b)
pipes (see popen), c) sockets with ad hoc protocols (see asyncchat
and SocketServer), d) predefined protocols (see Internet Protocols
and Services). The various "server" modules have the setup code
from "1" above builtin, so you can ignore that level of detail.
["see" references can be found in the normal python documentation's
Library Reference"].

3. Once you have a working daemon, you need a way to start and stop
it. You could manually start it each time you need it and then
"kill" it when done. More commonly you would do it (in Linux and
UNIX) via a boot-time initialization script (typically found in
/etc/init.d). Each *NIX has its own flavor of setting these up, so
you need to look at existing services and maybe copy-and-edit a
working script.

4. If security is an issue (it probably is), you may also want to hide
the service behind xinetd. That is a whole setup story in its own
right. And if security is really troublesome, you may need to
assure the python interpreter itself is safe. That kind of
consideration is way out of my league.


Jul 18 '05 #4

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,...
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...
7
by: Michael Ransburg | last post by:
Hi! I have implemented a daemon in C++. It runs all the time, between reboots. Is there a way for other C++ classes to get a reference to the instance of this daemon class in order to call its...
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...
2
by: kernel.lover | last post by:
Hello, I have UDP socket programs. I want to run server socket on one machine and client socket on another and viceversa. How can i create a flexibility at each system so that it will not only...
1
by: Stefan Neumann | last post by:
I have written a daemon which should run endlessly. The structure looks like this: - start-stop-daemon forks my python program then: if __name__=="__main__": try: main()
9
by: Sakagami Hiroki | last post by:
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, --...
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
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.