redirecting stdout and stderr for a windows service

Ian Hobson ian at ianhobson.co.uk
Mon Sep 6 10:29:44 EDT 2010


Hi all,

I am trying to redirect stdout and stderr on a python  windows service, 
so that the service will not stall after 256 chars is written, and so I 
can use print for simple debugging.

I have the following 4 lines (copy/pasted) in the source of my code.

if __name__ == '__main__':
     sys.stdout = sys.stderr = open("d:\logfile.txt", "a")
     print "Starting up"
     win32serviceutil.HandleCommandLine(PythonService)

When I run them as a free standing program, logfile.txt is created
the text placed in it, and it crashed on the last line - no permissions 
to create a service. (Micro-cluless has no sudo command in Win7. :( )

When I start the code as a service, it does not create the log file
does not put anything in it, but the last line is executed and the 
service starts! There are no Events on the event log.

When I stop the service, it stops. Still no log file. Still no events.

I tried running the service with my account and not the system user. 
This gave me "run as service" permissions, but did not alter the results.

What is happening?

And how can I set up logging so that the service can simply restart, 
leaving the trackback in the log file for me to come to later.

All help gratefully received. I've lost most of my remaining hair today 
on this one.

Ian










More information about the Python-list mailing list