Python-Windows-Service-Example icon indicating copy to clipboard operation
Python-Windows-Service-Example copied to clipboard

python executable app(using pyinstaller) service not starting after restart or user logins after shutdown.

Open mai1x9 opened this issue 4 years ago • 0 comments

Here is my code, which writes to file C:\\TestService.log every 1 second with counter value written to file. Counter value is increased by 1 every second/

python file name: example.py

import servicemanager
import socket
import sys
import win32event
import win32service
import win32serviceutil
import subprocess
import threading
# sc.exe create ABCTestService binpath= C:\Users\Mahi\Desktop\Experiments\sctask\dist\service.exe start= auto
class TestService(win32serviceutil.ServiceFramework):
    _svc_name_ = "ABCTestService"
    _svc_display_name_ = "ABC Test Service"
    _svc_description_ = "My ABC service description"

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

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

   def SvcDoRun(self):
        c = 0
        rc = None
        while rc != win32event.WAIT_OBJECT_0:
            with open('C:\\TestService.log', 'a') as f:
                f.write('test service running...{}\n'.format(c))
            c += 1
            rc = win32event.WaitForSingleObject(self.hWaitStop, 1000)


if __name__ == '__main__':
    if len(sys.argv) == 1:
        servicemanager.Initialize()
        servicemanager.PrepareToHostSingle(TestService)
        servicemanager.StartServiceCtrlDispatcher()
    else:
        win32serviceutil.HandleCommandLine(TestService)

After converting to exe using pyinstaller. I have executed,

example.py install

with admin rights to register the service. Then I have modifed the service type to auto (auto - Specifies a service that automatically starts each time the computer is restarted and runs even if no one logs on to the computer.) Now I have restarted my system, and even if I set the type=auto, the python script inside the SvcDoRun did not work.. The service itself did not start and it is in stopped state.

However If I manually, start the service, then it is writing to the file. Since it is infinite loop, the service is in running state. Now if I restart the system with the service in running state, the PID of the service is still the same as previous, and the counter value did not reset to 0.(Eg: Before I shutdown, the counter value was lets say 100, and it was written to file, now after restarting, I have checked the file and the counter value has resumed from 100 and started writing 101,102,...to file. Why did the counter value did not reset to 0 and start from 0, 1, 2.... Looks like pid of service is same though.

Also, If the shutdown signal is received, how to stop the service. My bad, start after system restarts or reboots did not work. Any ideas how to fix the issue.

@HaroldMills

mai1x9 avatar Dec 21 '21 14:12 mai1x9