Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > creating a daemon?

Reply
Thread Tools

creating a daemon?

 
 
Harry George
Guest
Posts: n/a
 
      01-08-2004
bmgx <(E-Mail Removed)> 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.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)
6-6M31 Knowledge Management
Phone: (425) 342-5601
 
Reply With Quote
 
 
 
 
bmgx
Guest
Posts: n/a
 
      01-08-2004
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..

 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      01-08-2004
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
 
Reply With Quote
 
bmgx
Guest
Posts: n/a
 
      01-08-2004
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 <(E-Mail Removed)> 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.
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Error Creating Control" when creating a custom control (Design Time). Can't see tooltip message. Ravi Ambros Wallau ASP .Net Web Controls 0 06-01-2005 02:36 PM
"Error Creating Control" when creating a custom control (Design Time). Can't see tooltip message. Ravi Ambros Wallau ASP .Net 0 06-01-2005 02:36 PM
Re: Creating a network with a single wireless computer Pavel A. Wireless Networking 1 08-03-2004 12:19 AM
Re: Creating a network with a single wireless computer Tim Levy Wireless Networking 0 08-02-2004 07:32 PM
creating Network Traffic Wireless Networking 1 07-06-2004 03:52 PM



Advertisments