Running a windows python service without pythonservice.exe




















It was very easy to migrate to it. After working on this on and off for a few days, here is the answer I would have wished to find, using pywin32 to keep it nice and self contained. This is complete working code for one loop-based and one thread-based solution. It may work on both python 2 and 3, although I've only tested the latest version on 2. The loop should be good for polling code, and the tread should work with more server-like code.

It seems to work nicely with the waitress wsgi server that does not have a standard way to shut down gracefully. I would also like to note that there seems to be loads of examples out there, like this that are almost useful, but in reality misleading, because they have cut and pasted other examples blindly.

I could be wrong. That said I still feel I'm on somewhat shaky ground here, especially with regards to how clean the exit from the thread version is, but at least I believe there are nothing misleading here. Use a simple flag to terminate thread. The important bit is that "thread done" prints.

For a more elaborate example exiting from an uncooperative server thread see my post about the waitress wsgi server. This answer is plagiarizer from several sources on StackOverflow - most of them above, but I've forgotten the others - sorry. It's simple and scripts run "as is". And it should work for all scripting languages Python, Perl, node. Now there's a bit of editing and you don't want all your services called 'pyGlue'.

So there's a script build. It is this '. Once installed you can set it to run automatically. Installation - copy the '. Run the '. Open Windows Services, as Adminstrator, and start you service. Now every server is different - different installations of Python, different folder structures. I maintain a folder for every server, with a copy of pyGlue. And I create a 'serverBuild. The accepted answer using win32serviceutil works but is complicated and makes debugging and changes harder.

You write and comfortably debug a normal python program and when it finally works you use NSSM to install it as a service in less than a minute:. From an elevated admin command prompt you run nssm.

By the way, if your program prints useful messages that you want to keep in a log file NSSM can also handle this and a lot more for you.

I know this is old but I was stuck on this forever. For me, this specific problem was solved by copying this file - pywintypes Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. How do you run a Python script as a service in Windows? Ask Question. Asked 13 years, 4 months ago.

Active 1 year, 2 months ago. Viewed k times. Improve this question. Hanno Fietz Hanno Fietz Add a comment. Active Oldest Votes. This is a basic skeleton for a simple service: import win32serviceutil import win32service import win32event import servicemanager import socket class AppServerSvc win32serviceutil.

CreateEvent None,0,0,None socket. ReportServiceStatus win32service. SetEvent self. LogMsg servicemanager. HandleCommandLine AppServerSvc Your code would go in the main method—usually with some kind of infinite loop that might be interrupted by checking a flag, which you set in the SvcStop method.

Improve this answer. Ricardo Reyes Ricardo Reyes After coding this, how do I tell Windows to run this as a service? Kit: run your script with the from the command line with the parameter "install". Then you'll be able to see your application in Windows' Services list, where you can start it, stop it, or set it to start automatically — Ricardo Reyes. You give special mention to pythoncom, and you import it in your example code.

The problem is you never actually use pythoncom anywhere in your example code, you only import it. Why give it special mention and then not show its usage?

Why for the socket. Is it needed for a service, or was it just accidentaly copied from some existing service? Show 17 more comments. GUI approach 1 - install the python program as a service. Install the service You must specify a name for the service, the path to the proper Python executable, and the path to the script: nssm.

One easy approach is to tell nssm that it needs to change to the proper starting directory, as you would do yourself when launching from a command shell: nssm. To start the service nssm. To stop the service nssm. Michael Ekoka 17k 9 9 gold badges 69 69 silver badges 78 78 bronze badges. Adriano P Adriano P 5 5 silver badges 4 4 bronze badges. I used to use nssm.

How to provide a virtual environment? Do not loose more time and go for NSSM approach. This allows you to install a service and provides a GUI for configuring the command line the service executes. For Python you can do this, which creates the service in one go:.

Note that the service doesn't run the Python script directly, it runs the Python interpreter and passes it the main script on the command line. Alternatively you can use tools provided in the Windows Server Resource Kit for your operating system version so create the service. This is a variation on the generic example. You just need to import your app script and invoke it's run method in the service's main function.

A Python script that can be run as a service The modules used in this example are part of pywin32 opens new window Python for Windows extensions. Hope it helps! Improve this answer. That will let you use pythonservice more easily. BuvinJ This fixed my error while using PythonService. Ah I see, you simply add it to the overall computer system path in the 'System Control Panel'. Personally I had to add anaconda as in snip2code. Initialize servicemanager.

StartServiceCtrlDispatcher else: win32serviceutil. Alex Alex 1, 10 10 silver badges 27 27 bronze badges. Alex M. Dominic Barraclough Dominic Barraclough 2 2 silver badges 7 7 bronze badges. This solution worked from me on python 3. Another helpful tip is to add the following line sys. That way you can get more helpful information from the service of what goes wrong. Jonathan Jonathan 7, 7 7 gold badges 48 48 silver badges 67 67 bronze badges.

I was unable to start python services too , I have python39 installed. Maris B. In the python module: """ main. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown.



0コメント

  • 1000 / 1000