pythonwindowspywin32

Pywin32 Windows Service Script Not Connecting with Main Function and Service Unable To Finish Starting?


I'm working on a python app that, among other things, works to block common RCA applications. Because I want it to be able to run on start up and in the background, I figured the best way was to make it into a Windows Service. I know you can do things manually, like schedule the task via task manager, but I want it to run without that user involvement, so I decided to use pywin32.

With that in mind, let me tell you my issue: Installing the python script as a service works fine, you can see it added to the windows service manager, but starting the service is where the error comes in.

Svc_manager.py, which is what controls the service, just continuously tries to call the main function in scamwatch_main.py and is unable to. It times out when starting and then the service stays stuck in "Starting" and I have to kill it from the task manager.

I even tried renaming the main file to see if there was any call confusion since there's a main function in both files, but that didn't work. I also tried moving the service code into the main file instead, but that gave me more errors and just overall made everything worse. I've also tried restarting my computer after installing the service.

Debugging logs have not helped outside of it staying at "calling main from scamwatch_main.py". Windows logs simply show that the service has started.

If you start the script up manually from the cmd line, it runs perfectly fine, so it has to be that connection from svg_manager.py, right? I feel like it's probably something simple that I'm just missing, but I have no idea.

Anyway, here's the svc_manager.py code:

# this file handles the pywin32 config for using ScamWatch as a service

import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
from scamwatch_main import scamwatch_main
import logging
import os

class SWWinService(win32serviceutil.ServiceFramework):
    _svc_name_ = "ScamWatchService"
    _svc_display_name_ = "ScamWatch Service"

    def __init__(self, args):
        win32serviceutil.ServiceFramework.__init__(self, args)
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
        socket.setdefaulttimeout(180)

        log_dir = 'Log Directory Path'
        os.makedirs(log_dir, exist_ok=True)
        log_file = os.path.join(log_dir, 'ScamWatch.log')

        logging.basicConfig(
            filename=log_file,
            level=logging.DEBUG,
            format='%(asctime)s %(levelname)s: %(message)s'
        )
        logging.info("ScamWatch initialized Successfully")

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.hWaitStop)
        logging.info('ScamWatch service stopping')

    def SvcRun(self):
        try:
            servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE, servicemanager.PYS_SERVICE_STARTED,
                                (self._svc_name_, ''))
            
            self.main()
        except Exception as e:
            logging.error(f"Exception in SvcRun: {e}")
            servicemanager.LogErrorMsg(f"Exception in SvcRun: {e}")
            self.SvcStop()
    
    def main(self):
        rc = None
        logging.info('ScamWatch service running')
        while rc != win32event.WAIT_OBJECT_0:
            try:
                logging.info("calling main from scamwatch_main.py")
                scamwatch_main.scamwatch_main()
            except Exception as e:
                logging.error(f"Exception in main loop: {e}")
            rc = win32event.WaitForSingleObject(self.hWaitStop, 3000)
        logging.info('Service stopped by exiting loop in svc_manager!')


if __name__ == '__main__':
    servicemanager.Initialize()
    servicemanager.PrepareToHostSingle(SWWinService)
    servicemanager.StartServiceCtrlDispatcher()

And then here's my scamwatch_main.py code. Could the admin priv check be messing with it?


# This is the main executable for ScamWatch.
import threading
import time
import sys
import logging
import os
from process_blocking import monitor_process, one_time_connection
from elevate import is_admin, run_as_admin
from port_blocking import block_all_ports

def scamwatch_main():

    logging.info("ScamWatch is running!")

    # Check if in admin mode. If not, restart as admin
    if not is_admin() and "--elevated" not in sys.argv:
        logging.info("Attempting to restart with administrative privileges...")
        if run_as_admin():
            logging.info("Restarted with administrative privileges.")
            sys.exit(0)  # Terminate the current process to restart as admin
        else:
            logging.error("Failed to restart with administrative privileges.")
            sys.exit(1)
    
    try:

        # Check and block common remote connection ports
        logging.info("Blocking remote connection ports if needed...")
        block_all_ports()

        # Begin monitoring processes for common RCA App exes
        logging.info("Now monitoring for RCA applications. Don't worry, you're safe :)")
        monitor_thread = threading.Thread(target=monitor_process)
        monitor_thread.daemon = True
        monitor_thread.start()

    except KeyboardInterrupt:
        logging.info("ScamWatch has been stopped by user!")

    except Exception as e:

        logging.error(f"Something went wrong! Here's the error info: {e}")


# Run this script as an executable, not as a file to import
if __name__ == "__main__":
    scamwatch_main()

It's a work in progress and needs to be cleaned up, but first priority is just to get the service to work lmao


Solution

  • class SWWinService(win32serviceutil.ServiceFramework):
        def SvcRun(self):
        # This is the entry point the C framework calls when the Service is
        # started. Your Service class should implement SvcDoRun().
    
        
    

    If the SvcRun method is not executed completely, it will always show "Starting" in the Service Manager.
    You should start the blocking function in a new thread.

    There are indeed many problems to face when using pywin32, whether for novices or experts.
    Mistakes are common when implementing a python program as a service, so I look for other solutions.
    I am currently using https://github.com/winsw/winsw to implement the service instead of pywin32.

    I have used the following code to successfully implement a python service, which you can refer to.

    win_service.py

    import servicemanager
    import win32event
    import win32service
    import win32serviceutil
    
    
    class PySvc(win32serviceutil.ServiceFramework):
        _svc_name_ = "svc_name"
        _svc_display_name_ = "svc_display_name"
        _svc_description_ = "svc_description"
    
        def __init__(self, args):
            win32serviceutil.ServiceFramework.__init__(self, args)
            self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
    
        def SvcDoRun(self):
            self.start() # before LogMsg
            servicemanager.LogMsg(
                servicemanager.EVENTLOG_INFORMATION_TYPE,
                servicemanager.PYS_SERVICE_STARTED,
                (self._svc_name_, ""),
            )
            win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)
    
        # called when we're being shut down
        def SvcStop(self):
            self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
            self.stop()
            win32event.SetEvent(self.hWaitStop)
            servicemanager.LogMsg(
                servicemanager.EVENTLOG_INFORMATION_TYPE,
                servicemanager.PYS_SERVICE_STOPPED,
                (self._svc_name_, ""),
            )
    
        def start(self):
            # should start in new thread
            pass
    
        def stop(self):
            pass
    

    win_demo.py

    import base64
    import json
    import re
    import sys
    import threading
    from hashlib import md5
    from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
    from urllib.parse import urlparse, unquote
    
        # 1. copy 'site-packages\pywin32_system32\pywintypes39.dll' to
        #   site-packages\win32
        # 2. pywin32 not load Python\Python39\site-packages
        # 3. Windows firewall allow "site-packages\win32\pythonservice.exe"
    sys.path.append(
        r"C:\virtualenvs\TJP2DPxi-py3.9\Lib\site-packages"
    )
    sys.path.append(
        r"C:\virtualenvs\TJP2DPxi-py3.9\Lib\site-packages\win32\lib"
    )
    import requests
    import yaml
    
    from win_service import PySvc
    
    
    class HttpRequestHandle(BaseHTTPRequestHandler):
        ...
    
    
    class LinkService(PySvc):
        _svc_name_ = "linkparse"
        _svc_display_name_ = "linkparse"
        _svc_description_ = "This service filter ss nodes"
    
        def start(self):
            self.server = ThreadingHTTPServer(("0.0.0.0", 5000), HttpRequestHandle)
            t = threading.Thread(target=self.server.serve_forever)
            t.start()
    
        def stop(self):
            self.server.shutdown()
    
    
    if __name__ == "__main__":
        # linkserver = ThreadingHTTPServer(('0.0.0.0', 5000), HttpRequestHandle)
        # linkserver.serve_forever()
        import win32serviceutil
    
        win32serviceutil.HandleCommandLine(LinkService)
    

    If you just want to implement a Windows service and are not limited to a certain method, I recommend you use winsw.
    Currently I am using WinSW v3.0.0-alpha.11 and following https://github.com/winsw/winsw?tab=readme-ov-file#use-winsw-as-a-bundled-tool.
    My xml config

    <service>
        <id>linkparse</id>
        <description>linkparse</description>
        <executable>python</executable>
        <arguments>-u wrapclash.py</arguments>
        <env name="PATH"
             value="C:\virtualenvs\TXyTCLxP-py3.12\Scripts;%PATH%"/>
        <depend>WlanSvc</depend>
        <onfailure action="restart" delay="5 sec"/>
        <logpath>logs</logpath>
        <log mode="roll-by-size">
            <sizeThreshold>5120</sizeThreshold>
            <keepFiles>3</keepFiles>
        </log>
        <prestart>
            <executable>cmd</executable>
            <arguments>/c "taskkill /T /F /im whateveryouwant"
            </arguments>
        </prestart>
    </service>