Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 410
Michael Esteves Avatar asked Dec 29 '11 10:12

Michael Esteves


People also ask

How do I run a program as a service in Windows?

PathToExecutable: Type the full path of the application that you want to run as a Windows service. For example: To install the "Notepad.exe" application as a Windows service with the name "Notepad", give the following command: RunAsService install "Notepad" "C:\Windows\System32\notepad.exe"

How do I run a .PY file in CMD?

Using the python Command To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World!

Can you run .PY on Windows?

On Windows, the standard Python installer already associates the . py extension with a file type (Python. File) and gives that file type an open command that runs the interpreter ( D:\Program Files\Python\python.exe "%1" %* ). This is enough to make scripts executable from the command prompt as 'foo.py'.


1 Answers

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.

like image 59
Ohad Avatar answered Oct 13 '22 16:10

Ohad