The Core Technologies Blog

Professional Software for Windows Services / 24×7 Operation

Q&A: How Do I See the Output/Traceback From my Python Windows Service?

Q&A - Python Output
  We’re using AlwaysUp to run a Python program as a service. Do you have any recommendations about logging?

Ideally I would see the output of the python program as if it were run in command line, to see the traceback if there is an error. For example:

    Traceback (most recent call last):
    File "", line 1, in 
    NameError: name 'r' is not defined

— Liam

Hi Liam. I see your problem. It’s important to know when Python runs into a problem, but errors can be difficult to spot when the service’s console — alive and well in the isolated Session 0 — isn’t visible on your desktop.

You have a couple of options:

1. Capture the output of your Python program in a text file

AlwaysUp can capture Python’s command line output to a file of your choosing. That option is available on the Extras tab:

Capture Output from the Extras tab

Check the Capture output to this log file box and enter the full path to a (new) text file.

If your script generates lots of text, you may want to prevent the file from growing too large. Check the Automatically trim box and specify the maximum size in megabytes. When the file grows to the maximum size, the oldest 10% will be discarded to make room for new entries. Be sure to tune the max size so that you don’t lose useful data.

With those settings in place, your Python service will write all console/traceback output to the file. Both the standard output and standard error streams will be captured.

Open the file to see what’s going on with your script. Or even better, use the free Tail for Win32 to “track” the file. New lines will appear in real-time — just if you were looking at the Python console.

To confirm our advice, we ran a Python script with a deliberate error (borrowed from this page) and captured the output. Here is the result (with WinTail monitoring the output file):

Python script: Traceback output

It works!

2. Run your Python script in the current session, where you will see the console

If you don’t want to configure logging — or you just want to see the console temporarily while debugging a problem — you can instruct AlwaysUp to run the Python console on your desktop.

Simply select Application > Restart in this session and AlwaysUp will temporarily stop Python and “re-parent” it onto your screen:

Restart Python program in this session

Your program will run visibly while you are logged in. When you logout, AlwaysUp will automatically return your application to the background (i.e. Session 0).

One final bit of advice…

We recommend that you do your best to thoroughly debug your Python script before running it as a set-it-and-forget-it Windows Service. Dynamic languages like Python need extra attention in that area.

The fewer surprises in production the better, right?

Posted in AlwaysUp | Tagged , , , , , | Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *