Michael Esteves
Michael Esteves

Reputation: 1485

Start python .py as a service in windows

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

Upvotes: 8

Views: 21584

Answers (3)

user285594
user285594

Reputation:

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

Upvotes: 0

Ohad
Ohad

Reputation: 2618

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.

Upvotes: 17

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

Upvotes: 0

Related Questions