How to make a Python script (installed as a service) to survive as a result of logout?

I followed the instructions in this about writing a Python script to be used as a service. I put the loop code in def main().

I installed the service using python my_script.py install. I managed to start and stop the service through services.mscin Windows XP.

This is a logging program that is designed to record logs while Windows is running. He does not need to care about who or who enters or exits the system. My problem is that the service will stop when logging out . How to make it withstand a logout?

+3
source share
1 answer

The system generates the signals CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT and CTRL_SHUTDOWN_EVENT when the user closes the console, logs out or shuts down the system so that the process can clear before completion. To ensure that the service is disconnected from all this, you will need to use a control handler this way:

  win32api.SetConsoleCtrlHandler(lambda x: True, True)  

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

Just checked that there is a recipe that illustrates this very well.

+2
source

Source: https://habr.com/ru/post/1766299/


All Articles