Skip to content

Instantly share code, notes, and snippets.

@guillaumevincent
Last active August 6, 2024 08:30
Show Gist options
  • Save guillaumevincent/d8d94a0a44a7ec13def7f96bfb713d3f to your computer and use it in GitHub Desktop.
Save guillaumevincent/d8d94a0a44a7ec13def7f96bfb713d3f to your computer and use it in GitHub Desktop.
Windows Service with Python 3.5 and pyinstaller

TestService

Windows Service with Python 3.5 and pyinstaller

Requirements

Check

(env)$ python -V
Python 3.5.2

(env)$ pip freeze
PyInstaller==3.2

Build

(env)$ pyinstaller -F --hidden-import=win32timezone WindowsService.py

Run

(env) dist\WindowsService.exe install
Installing service TestService
Service installed

(env) dist\WindowsService.exe start
Starting service TestService

Clean

(env) dist\WindowsService.exe stop
(env) dist\WindowsService.exe remove
import servicemanager
import socket
import sys
import win32event
import win32service
import win32serviceutil
class TestService(win32serviceutil.ServiceFramework):
_svc_name_ = "TestService"
_svc_display_name_ = "Test Service"
_svc_description_ = "My 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):
rc = None
while rc != win32event.WAIT_OBJECT_0:
with open('C:\\TestService.log', 'a') as f:
f.write('test service running...\n')
rc = win32event.WaitForSingleObject(self.hWaitStop, 5000)
if __name__ == '__main__':
if len(sys.argv) == 1:
servicemanager.Initialize()
servicemanager.PrepareToHostSingle(TestService)
servicemanager.StartServiceCtrlDispatcher()
else:
win32serviceutil.HandleCommandLine(TestService)
@dishantrathi
Copy link

Can anyone please help me i'm getting this error 1053 tried google and every thing i'm quit hopeless

Same is happening with me! @guillaumevincent

@guillaumevincent
Copy link
Author

guillaumevincent commented Apr 18, 2020

can you give us more info?
like the python version, any logs, etc?
as I said before, I don't have a window computer any more, so it's difficult to test

@barathmeenachisundaram
Copy link

Can anyone please help me i'm getting this error 1053 tried google and every thing i'm quit hopeless

Same is happening with me! @guillaumevincent

Same thing happened to me. What happened is, I checked whether it is working in as normal python file . so did python . So it told me servicemanager module missing. So installed it and tried again. Then i got this issue.
After lots of research, what i found is its because of wrong package being installed. So to resolve this, i created an new environment, and then did python install directly, instead of running it like a normal python file. It resolved my problem. Try this

@rickych1123
Copy link

It works like a charm!

@zohaib055
Copy link

how to start service auto ?

@scialuppa
Copy link

It works, but you could tell me why in the event log Service Control Manager generates an alert with id 7039.

"A service process other than the one started by the Service Control Manager connected when the TestService service started. The Service Control Manager started the 6112 process and the 6480 process connected.

Note that if the service is configured to start inside a debugger, this behavior is expected. "

@guillaumevincent
Copy link
Author

To be honest I don't know

@97k
Copy link

97k commented Jul 18, 2020

Hi @guillaumevincent
Thank you for sharing this.

I have created a service and it is running fine. However, I am trying to build an installer.exe that will help end-user to install the service using the installation wizard of windows.

I am looking the web but couldn't get anything useful.

Can anyone help?

@wasimafser
Copy link

I have created a service and it is running fine. However, I am trying to build an installer.exe that will help end-user to install the service using the installation wizard of windows.

https://youtu.be/_lmFArB6OI8 : watch from 03:00

@97k
Copy link

97k commented Jul 18, 2020

I have created a service and it is running fine. However, I am trying to build an installer.exe that will help end-user to install the service using the installation wizard of windows.

https://youtu.be/_lmFArB6OI8 : watch from 03:00

Hi @wasimafser

Thanks for the link, but I've already created the service exe and service is running just fine.
What I want is

  • to create a setup.exe installation wizard which the end user will install it just like any other software
  • After the user installs the service via setup.exe, the service will auto start at startup

I'm not able to make the installer for service that I've created

@wasimafser
Copy link

@97k i'm extremely sorry, I got it wrong, BTW, i would create another executable which will be the installer, you could do it with python if you want, with it you could run commands required to install the service your way (example.. use subprocess and "sc install ..."). Bundle your service executable with this installer and distribute.
let me know if you find a better way .

@Minobi
Copy link

Minobi commented Jul 18, 2020

What I want is

  • to create a setup.exe installation wizard which the end user will install it just like any other software
  • After the user installs the service via setup.exe, the service will auto start at startup

I'm not able to make the installer for service that I've created

@97k, maybe Inno setup or NSIS installers be suitable for this. I didn't do it myself, but I think they can be added to build process via PyInstaller build hooks.

@97k
Copy link

97k commented Jul 23, 2020

Hi @wasimafser, @Minobi!
I've solved the problem using Inno setup

I created a new exe file using pyinstaller that will take care of parameters -start, stop, remove
and used this new exe file with Inno setup.

Thanks for taking out time 😃

@Russell-Tran
Copy link

great! I found many other examples not work. but this short one work! with python 3.6.2 and pyinstaller 3.3.1

Confirming this works with python 3.6.2 and pyinstaller 3.3.1. Didn't work with Python 3.9 for me

@Saleem344
Copy link

Hi @wasimafser, @Minobi!
I've solved the problem using Inno setup

I created a new exe file using pyinstaller that will take care of parameters -start, stop, remove
and used this new exe file with Inno setup.

Thanks for taking out time 😃

Can send the process how you have used Inno setup.

@Saleem344
Copy link

Hi Everyone I'm facing an issue while running a python script as a windows service can anyone help me??
Issue:

  1. I have created a python script for windows service, service is creating I can't able to run I'm getting the following error ( The pythonftp service terminated with the following service-specific error: Incorrect function.) in windows logs.
  2. I'm able to run the service from windows services, only if I'm creating service from the environment this is also working only on my laptop the same I have tried on another laptop I'm getting the same error.
  3. I'm sharing a screenshot for reference.
  4. code link: https://lnkd.in/gsuRPyh
  5. script name ftpservice.py
    #python3 #windows

error

@KaryakarteVK
Copy link

KaryakarteVK commented May 23, 2021

Hi @wasimafser, @Minobi!
I've solved the problem using Inno setup

I created a new exe file using pyinstaller that will take care of parameters -start, stop, remove
and used this new exe file with Inno setup.

Thanks for taking out time 😃

Hey @97k, I am searching for this but not found anything useful till now. You came up with the solution,can share the details or the link which you referred

@Russell-Tran
Copy link

Hi @wasimafser, @Minobi!
I've solved the problem using Inno setup
I created a new exe file using pyinstaller that will take care of parameters -start, stop, remove
and used this new exe file with Inno setup.
Thanks for taking out time 😃

Hey @97k, I am searching for this but not found anything useful till now. You came up with the solution,can share the details or the link which you referred

Hi @KaryakarteVK , if it’s any help, I’ve found NSSM easiest to use after trying various alternatives, and specifically this CLI-based answer on Stack Overflow: https://stackoverflow.com/a/46450007/14775744

I use it in my project here (there are some idiosyncrasies about NSSM I address): https://github.com/Salk-Harnessing-Plants-Initiative/greenhouse-giraffe-uploader#part-3-run-automatically-as-a-service

@pauk163rus
Copy link

@guillaumevincent, hello man. thx for sharing this, it's very helpful. I have a little problem with relative path ... i mean, if i use your example:
"with open('C:\TestService.log', 'a') as f:" all works fine, but if i set something like: "with open('.\TestService.log', 'a') as f:" no TestService.log is created. I'm running w10. Thx

@Minobi
Copy link

Minobi commented Jul 26, 2021

Hi @pauk163rus, I think this is because current working directory for windows services is C:\Windows\system32 and your log file with relative path created there

@pauk163rus
Copy link

Hi @pauk163rus, I think this is because current working directory for windows services is C:\Windows\system32 and your log file with relative path created there

thx dude, you were right - my logs were really here: C:\Windows\SysWOW64...........

@roykhoury
Copy link

roykhoury commented Aug 21, 2021

@guillaumevincent
i'm getting "Error installing service: access is denied. (5)"
When trying to install the service...
Any idea why that is? :o

@roykhoury
Copy link

Nevermind, I just had to run it from an Admin Terminal!
Thanks :)

@mai1x9
Copy link

mai1x9 commented Dec 21, 2021

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.
@guillaumevincent

@guillaumevincent
Copy link
Author

As I said previously, I'm sorry I haven't used a windows environment for years
I cannot test this code anymore

@nassimmz
Copy link

Hello
Thanks for the script, works perfectly
is there a way to separate it into 2 files: one which is the main script in which the work is done and calls the TestService file and the other is the TestService

I tried doing it but it doesn't seem to work for me, any help would be much appreciated

@guillaumevincent
Copy link
Author

I don't understand the question.
You can try to import your python module (something like: from . import foo, and call the methods you want in SvcDoRun with foo.do_something() no?

If it's not working you need to make the import working with pyinstaller

@MirasSafadi
Copy link

Thank you for this script.
You cannot believe how much time I spent on this simple task!
I was getting 1053 error every time, I tried EVERY solution I found on google, until I found yours.
Thanks!!

@jrialland
Copy link

Thx, awesome work !

@arkitpatel
Copy link

Thanks for the efforts! If anyone is wondering if it still works then yes it does. Tested with Python 3.12.4, pywin32 306 and pyinstaller 6.9.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment