How do you run a Python script as a service in Windows?

后端 未结 13 1647
你的背包
你的背包 2020-11-22 02:18

I am sketching the architecture for a set of programs that share various interrelated objects stored in a database. I want one of the programs to act as a service which prov

13条回答
  •  旧时难觅i
    2020-11-22 02:32

    There are a couple alternatives for installing as a service virtually any Windows executable.

    Method 1: Use instsrv and srvany from rktools.exe

    For Windows Home Server or Windows Server 2003 (works with WinXP too), the Windows Server 2003 Resource Kit Tools comes with utilities that can be used in tandem for this, called instsrv.exe and srvany.exe. See this Microsoft KB article KB137890 for details on how to use these utils.

    For Windows Home Server, there is a great user friendly wrapper for these utilities named aptly "Any Service Installer".

    Method 2: Use ServiceInstaller for Windows NT

    There is another alternative using ServiceInstaller for Windows NT (download-able here) with python instructions available. Contrary to the name, it works with both Windows 2000 and Windows XP as well. Here are some instructions for how to install a python script as a service.

    Installing a Python script

    Run ServiceInstaller to create a new service. (In this example, it is assumed that python is installed at c:\python25)

    Service Name  : PythonTest
    Display Name : PythonTest 
    Startup : Manual (or whatever you like)
    Dependencies : (Leave blank or fill to fit your needs)
    Executable : c:\python25\python.exe
    Arguments : c:\path_to_your_python_script\test.py
    Working Directory : c:\path_to_your_python_script
    

    After installing, open the Control Panel's Services applet, select and start the PythonTest service.

    After my initial answer, I noticed there were closely related Q&A already posted on SO. See also:

    Can I run a Python script as a service (in Windows)? How?

    How do I make Windows aware of a service I have written in Python?

提交回复
热议问题