Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

redirecting stdout and stderr for a windows service

489 views
Skip to first unread message

Ian Hobson

unread,
Sep 6, 2010, 10:29:44 AM9/6/10
to pytho...@python.org
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

Ulrich Eckhardt

unread,
Sep 6, 2010, 11:17:46 AM9/6/10
to
Ian Hobson wrote:
> sys.stdout = sys.stderr = open("d:\logfile.txt", "a")

"\l" is probably not what you want. Consider using "\\l" or r"\l" instead.

Uli

--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

Ian

unread,
Sep 6, 2010, 5:33:50 PM9/6/10
to pytho...@python.org
On 06/09/2010 15:29, Ian Hobson wrote:
> Hi all,
>
Forget this.

The problem is that it is a Windows Service, so it is not initialised in
the normal way.

PythonService.exe loads other code (named __main__) that loads the
service proper, so the
if test was never true!

Regards

Ian

0 new messages