redirecting stdout and stderr for a windows service

Discussion in 'Python' started by Ian Hobson, Sep 6, 2010.

  1. Ian Hobson

    Ian Hobson Guest

    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"

    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 Hobson, Sep 6, 2010
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.