Running a script as a daemon at boot time

Chris Liechti cliechti at gmx.net
Wed May 29 20:19:34 EDT 2002


"Engel, Gregory" <Gregory_Engel at csgsystems.com> wrote in
news:mailman.1022710248.7714.python-list at python.org: 
> I've a script which in part serves as an HTTP server.  I can run this
> as a background process when logged on as myself.  I need to find a
> way to have the script run as a daemon at boot time.  I have written a
> script for /etc.rc.d/init.d and it responds well to start/stop
> commands.  However, when a GET request arrives at the server, the
> process dies without generating an error (at least that I've been able
> to trap.)  The do_GET is as follows: 
>             self.ServerLog('Mark 1') # This line gets logged...
> 
>             self.send_response(200)
> 
>             self.ServerLog('Mark 1') # This line DOES NOT get
>             logged... 

i think send_response writes a message to sys.stderr. do you have closed 
that file?

you can redirect sys.stderr and and sys.stdout to a log file or replace 
them with a NIL writer, but don't close it:

class NIL:
    	def write(self, s):
    	    	pass

sys.stdout = sys.stderr = NIL()

or:

sys.stdout = sys.stderr = open("/var/log/mydaemonlog","w")


redirecting stderr in a logfile is useful - that way you have the traceback 
from exceptions logged...

just had it in the clipboard from an other message... this shows how to 
decouple the script to be runas a demon:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012

chris


-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list