Start python .py as a service in windows

主宰稳场 提交于 2019-12-02 19:43:12

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.

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 :)

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

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