Start python .py as a service in windows

故事扮演 提交于 2019-12-03 05:18:09

问题


I've created a windows service to start a .py script.

sc create "Maraschino" binPath= "C:\HTPC\Maraschino\maraschino-cherrypy.py" DisplayName=    "Maraschino" depend= "Tcpip"

Then I've added a registry key to link the .py to open using python.exe

Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Maraschino\Parameters]
"AppDirectory"="C:\\Python27"
"Application"="C:\\Python27\\python.exe C:\\HTPC\\Maraschino\\maraschino-cherrypy.py"

However when I try start the service I get Error 193 0xc1 which when googled revealed that it isn't a valid exe I'm trying to start. I know its not an .exe but a .py and linking it to open with python.exe should fix this but I'm making an error. Does anyone have any insight into what I might be doing wrong when linking the script to use python.exe

Thanks


回答1:


You can do this using the srvany.exe, which is a tool from Microsoft dedicated for this kind of tasks.

First, download and install the Windows Resource Kit. Note: You only need srvany.exe, which works on all versions of Windows.

Presuming that the Windows Resource Kit was installed at C:\Program Files\Windows Resource Kits\ run:

sc create "[YourService]" binPath= "C:\Program Files\Windows Resource Kits\srvany.exe"

Now, run regedit.

In the Registry Editor dialog select HKEY_LOCAL_MACHINE > SYSTEM > CurrentControlSet > Services > [YourService]

With [YourService] selected, hit Edit > New > Key from the toolbar.

Type Parameters and hit enter.

From the toolbar select Edit > New > String Value.

Type Application and hit enter.

Right-click Application and select Modify.

C:\Python27\python.exe C:\[YourServicePath].py

Hit the OK button.

And boom! you have a nice new service.




回答2:


I don't know how sc works, but i think that must be some way to pass parameters to the binary, so you could try to register "C:\Python27\python.exe C:\HTPC\Maraschino\maraschino-cherrypy.py" instead.

You could also try py2exe :)




回答3:


Updates of @Ohad. First of all srvany.exe is to be deployed to all machines

Step 1:

  • Download and install Windows Resource Kit.
  • Which was found in my box: C:\Program Files (x86)\Windows Resource Kits\Tools\srvany.exe .
  • Then open command prompt and hit

    sc create "[YourService]" binPath="C:\Program Files (x86)\Windows Resource Kits\Tools\srvany.exe" start=auto DisplayName="[YourService Monitor]"

    [SC] CreateService SUCCESS

Step 2: make a file.reg with following contents and double click on it

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\[YourService]\Parameters]
"Application"="C:\\[YourService Executable].exe"

Step 3: now start service and it will execute what-ever you have assigned in the file.reg

Done



来源:https://stackoverflow.com/questions/8666373/start-python-py-as-a-service-in-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!