views:

58

answers:

1

I followed the instructions in this answer about writing a Python script to be used as a service. I placed my looping code in def main().

I installed the service with python my_script.py install. I was able to Start and Stop the service through services.msc in Windows XP.

It's a logging program that is intended to write logs as long as Windows is running. It shouldn't care about who or whether anyone logs in or out. My problem is that the service will stop when I logout. How do I make it survive logouts?

+2  A: 

The system generates CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT, and CTRL_SHUTDOWN_EVENT signals when the user closes the console, logs off, or shuts down the system so that the process has an opportunity to clean up before termination. To ensure that you detach your service from all this, you will need to use the control handler to set it that way:

  win32api.SetConsoleCtrlHandler(lambda x: True, True)  

Check out : http://msdn.microsoft.com/en-us/library/ms685049(v=VS.85).aspx

Just checked out that there is recipe that illustrates this very well for you.

pyfunc
Where should this line come in? Am I right in saying that it should be in the class `__init__` in this [code snippet](http://stackoverflow.com/questions/32404/can-i-run-a-python-script-as-a-service-in-windows-how/32440#32440)?
Kit
@Kit : See my edited answer above. I just saw that there is a python recipe that should help you in all this.
pyfunc