473,756 Members | 7,560 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2519
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********@mgl eesonprop.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.stre rror)
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_i n.fileno(), sys.stdin.filen o())
os.dup2(child_o ut.fileno(), sys.stdout.file no())
os.dup2(child_o ut.fileno(), sys.stderr.file no())

#---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.stre rror)
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********@mgl eesonprop.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.stre rror)
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_i n.fileno(), sys.stdin.filen o())
os.dup2(child_o ut.fileno(), sys.stdout.file no())
os.dup2(child_o ut.fileno(), sys.stderr.file no())

#---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.stre rror)
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
5662
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
1
2142
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. ...
7
4241
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 methods directly? E.G.: 1) System Start - daemon class is instantiated and launched
1
3561
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...
2
2602
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 listen from client socket but will also communicate to another server socket. For that i think it require me to make both programs daemon so that they will run as backgroung hidden process. How to do that on redhat linux 9 machnes with i386 arch?
1
1989
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
9147
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, -- Sakagami Hiroki
3
4289
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
3988
by: Johny | last post by:
Is it possible to run a Python program as daemon? Thanks
0
10046
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
9886
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
9857
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
9722
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8723
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...
0
6542
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3817
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
2
3369
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.