Windows Service with Python 3.5 and pyinstaller
- Python 3.5.x
- Visual C++ Build Tools 2015
- PyInstaller 3.2
(env)$ python -V
Python 3.5.2
(env)$ pip freeze
PyInstaller==3.2
(env)$ pyinstaller -F --hidden-import=win32timezone WindowsService.py
(env) dist\WindowsService.exe install
Installing service TestService
Service installed
(env) dist\WindowsService.exe start
Starting service TestService
(env) dist\WindowsService.exe stop
(env) dist\WindowsService.exe remove
Here is my code, which writes to file
C:\\TestService.log
every 1 second with counter value written to file. Counter value is increasedby 1 every second/
python file name: example.py
After converting to exe using pyinstaller.
I have executed,
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.
@guillaumevincent