creating python daemon ?

vineet daniel vineetdaniel at gmail.com
Thu Sep 23 01:30:56 EDT 2010


On Sep 22, 2:20 pm, de... at web.de (Diez B. Roggisch) wrote:
> vineet daniel <vineetdan... at gmail.com> writes:
> > On Sep 21, 9:47 pm, de... at web.de (Diez B. Roggisch) wrote:
> >> vineet daniel <vineetdan... at gmail.com> writes:
> >> > Hi
>
> >> > I have succesfully created daemon with python script and as next step
> >> > I am trying to give input to that python script daemon from Apache
> >> > Logs....here I have got stuck and I have even checked IRC python
> >> > channel for solution. Apache is able to call the file but fails to
> >> > execute it properly and I get this error continuosly :
>
> >> > piped log program '/var/www/html/test.py' failed unexpectedly
>
> >> > How do I rectify the above error and make adjustment to the code so
> >> > that it takes input from apache.
>
> >> > code that I am using is as follows :
>
> >> > #! /usr/bin/env python
> >> > import sys,os
> >> > pid = os.fork()
> >> > os.chdir("/var/www/html/")
> >> > os.setsid()
> >> > os.umask(0)
> >> > #this line will read apache log in real time as redirected from the
> >> > CustomLog directive of Apache.
> >> > log = sys.stdin.readlines()
> >> > f = open('/var/www/logmongo.txt','a') # append log to this text file
> >> > f.write(log)
>
> >> > I'd appreciate if anybody could share the code that they used for
> >> > daemon or used with Apache CustomLog directive.
>
> >> The above code looks errornous - you don't check for the return-value of
> >> PID & take appropriate action. Like
>
> >> There is a daemonization recipe on active-state, which works nicely for
> >> me.
>
> >>http://code.activestate.com/recipes/278731-creating-a-daemon-the-pyth...
>
> >> Diez
>
> > Hi Diez
>
> > Thanks for pointing that out.
> > Ever tried giving input to such python daemons from a dynamic source
> > like Apache logs which get generated in real time. I want apache to
> > directly write to this python daemon which in turn will process the
> > logs the way I want. Any help will help me immensely.
>
> I don't understand the question. How is that input produced? How shall
> the two processes interact? Through some logfile one writes, the other
> reads? Through RPC-mechanisms? Sockets? Pipes?
>
> The fact that process B is a daemon or not has nothing to do with this
> whatsoever, btw.
>
> Diez

Hi Diez

There is a directive in Apache - CustomLog which can redirect logs to
a script or another file by using Pipe. I googled about it and came to
know that this works flawlessly with Perl as Apache treats Perl
scripts as daemon, for Python to take input from Apache, it needs to
work as daemon. Apache will then push its logs lines to this daemon as
input. Perl takes this input as <stdin> but python's stdin is not
working as expected it just takes the file name as given in CustomLog
directive using Pipe i.e if I use CustomLog '|/var/wwww/script.py'
combined, what script reads is just the file name, how and why I am
not able to figure it out. And thats the reason I am here. I hope
you've got what I am trying to do.



More information about the Python-list mailing list