Slazenger
Slazenger

Reputation: 43

Python :Unable to start installed windows service

I have created a windows service in python and then convert it into an exe file and install that exe file as a windows service using this command :

sc create PC-Service binpath= "C:\\Users\\Slazenger\\Desktop\\auto-py-to-exe-master\\output\\testing.exe" DisplayName= "PC-Service" start=auto 

and i got success message

[SC] CreateService SUCCESS 

But when i tried to install service with this command:

sc start PC-Service

I got this error message

[SC] StartService FAILED 1053:
The service did not respond to the start or control request in a timely fashion.

I don't know why I am getting this because when i run my python script as a service it runs perfectly but when i tried to run exe file as a service i am getting error kindly help me. Here is my python script:

def WriteToFile():

    while True:
        file = open ( "C:\\file.txt" , "w" )
        now = datetime.now()
        now = now.strftime("%B %d, %y %H:%M:%S")
        file.write(now)

class Pythonservice(win32serviceutil.ServiceFramework):


    _svc_name_ = 'PC-Service'
    _svc_display_name_ = 'PC-Service'
    _svc_description_ = 'Freindly Service'

    @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):
        self.isrunning = True

    def stop(self):
       self.isrunning = False

    def main(self):
        WriteToFile()



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

Kindly help me to rid out of this Thank you.

Upvotes: 1

Views: 930

Answers (1)

programmer
programmer

Reputation: 124

I have made some changes to your code I hope after that it will start working.

class MyService:
    def stop ( self ) :
        
        self.running = False

    def run ( self ) :
        
        self.running = True
        while self.running :
            WriteToFile()
class MyServiceFramework(win32serviceutil.ServiceFramework):
    _svc_name_ = 'PC-Service'
    _svc_display_name_ = 'PC-Service '

    def SvcStop ( self ) :
        
        self.ReportServiceStatus ( win32service.SERVICE_STOP_PENDING )
        self.service_impl.stop ( )
        self.ReportServiceStatus ( win32service.SERVICE_STOPPED )

    def SvcDoRun ( self ) :
        
        self.ReportServiceStatus ( win32service.SERVICE_START_PENDING )
        self.service_impl = MyService ( )
        self.ReportServiceStatus ( win32service.SERVICE_RUNNING )
        
        self.service_impl.run ( )



def init():
    if len ( sys.argv ) == 1 :
        servicemanager.Initialize ( )
        servicemanager.PrepareToHostSingle ( MyServiceFramework )
        servicemanager.StartServiceCtrlDispatcher ( )
    else :
        win32serviceutil.HandleCommandLine ( MyServiceFramework )

if __name__ == '__main__':
    init()

Now just convert it into an exe file using pyinstaller.

pyinstaller --noconfirm --onefile --windowed --hidden-import win32timezone path-to-py file

Then install your exe file as a service so go to the directory where exe file located usually it goes in dist folder.

yourexefile.exe --startup delayed install

you can start it using:

yourexefile.exe start

That's all hopefully this will work for you.

Upvotes: 1

Related Questions