creating python daemon ?

Brian Jones bkjones at gmail.com
Sun Sep 26 14:55:53 CEST 2010


I don't believe you need to be doing all kinds of acrobatics with apache and
your python process. On Linux and Unix machines, you can tell the stock
syslog daemon that messages sent to a certain syslog facility are to be sent
to a named pipe. So, if you tell syslog that, say, the local6 facility
should go to a named pipe, then point apache at that to do its logging,
 then your script becomes as simple as reading from the named pipe.

I did something years ago similar to this to get syslog messages sent to my
jabber client. It's perl (it predates my conversion to python), but doing
the same in python would be way easier :)

The code is here:

http://linuxlaboratory.org/snippets/2/

And an old article from Linux.com which explains the syslog bits is here:

http://www.linux.com/archive/feature/36631

I occasionally still hear from people that all of it still works.
hth.
brian

On Sun, Sep 26, 2010 at 5:20 AM, Stefan Schwarzer <sschwarzer at sschwarzer.net
> wrote:

> Hi Daniel,
>
> On 2010-09-23 07:30, vineet daniel wrote:
> > 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:
> >>>>> 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 guess you'll need some loop to process the input from
> Apache.
>
> > 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'
>
> Are the _four_ ws intentional?
>
> > 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.
>
> Do you have a line for the interpreter at the top of your
> script, like
>
> #!/usr/bin/python
>
> Is the script executable (e. g. chmod +x script.py)?
>
> Stefan
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Brian K. Jones
My Blog          http://www.protocolostomy.com
Follow me      http://twitter.com/bkjones
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100926/e10354bd/attachment.html>


More information about the Python-list mailing list