How to change the path of the executable using pywin32?

Asked

Viewed 19 times

0

I have two files: one that is the base and the other the 'main' that makes generate the service in windows; However, when I try to start the service, the path of the executable is like another

Does anyone know how I do this modification using pywin32?

that’s the basis:

import socket

import win32serviceutil

import servicemanager
import win32event
import win32service
import logging
import sys

logging.basicConfig(filename="SMWinservice.py", level=logging.DEBUG)

class SMWinservice(win32serviceutil.ServiceFramework):
   

    _svc_name_ = 'Extrator Service'
    _svc_display_name_ = 'Extrator Service'
    _svc_description_ = 'Python Service Description'

    @classmethod
    def parse_command_line(cls):
      
        win32serviceutil.HandleCommandLine(cls)

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

    def SvcStop(self):
        
        self.stop()
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.hWaitStop)

    def SvcDoRun(self):
       
        self.start()
        servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                              servicemanager.PYS_SERVICE_STARTED,
                              (self._svc_name_, ''))
        self.main()

    def start(self):
      
        pass

    def stop(self):
      
        pass

    def main(self):
        
        pass

# entry point of the module: copy and paste into the new module
# ensuring you are calling the "parse_command_line" of the new created class
if __name__ == '__main__':
    SMWinservice.parse_command_line()
    if len(sys.argv) == 1:
        servicemanager.Initialize() 
        servicemanager.PrepareToHostSingle(SMWinservice)

this is the main:

import time
from datetime import datetime
from pathlib import Path
from SMWinservice import SMWinservice
import psutil


    
class Extrator(SMWinservice):
    _svc_name_ = "Extrator"
    _svc_display_name_ = "Extrator"
    _svc_description_ = "Extrator de dados TARGIT"

    def start(self):
        self.isrunning = True

    def stop(self):
        self.isrunning = False

    def main(self):
        Extrator(SMWinservice)
        

     
if __name__ == '__main__':
    Extrator.parse_command_line()
    
    

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.