如何在Windows中将Python脚本作为服务运行?

Han*_*etz 245 python windows cross-platform

我正在描绘一组程序的架构,这些程序共享存储在数据库中的各种相互关联的对象.我希望其中一个程序充当服务,为这些对象的操作提供更高级别的接口,以及通过该服务访问对象的其他程序.

我目前的目标是将Python和Django框架作为实现该服务的技术.我很确定我想知道如何在Linux中守护Python程序.但是,它是系统应支持Windows的可选规范项.我对Windows编程没什么经验,也没有Windows服务的经验.

是否可以将Python程序作为Windows服务运行(即在没有用户登录的情况下自动运行)?我不一定要实现这一部分,但我需要大致了解如何做以决定是否按照这些方式进行设计.

编辑:感谢目前为止的所有答案,它们非常全面.我想知道一件事:Windows如何了解我的服务?我可以使用本机Windows实用程序进行管理吗? 在/etc/init.d中放置一个启动/停止脚本相当于什么?

Ric*_*yes 242

是的你可以.我使用ActivePython附带的pythoncom库或者可以使用pywin32(Python for Windows扩展)安装.

这是简单服务的基本框架:

import win32serviceutil
import win32service
import win32event
import servicemanager
import socket


class AppServerSvc (win32serviceutil.ServiceFramework):
    _svc_name_ = "TestService"
    _svc_display_name_ = "Test Service"

    def __init__(self,args):
        win32serviceutil.ServiceFramework.__init__(self,args)
        self.hWaitStop = win32event.CreateEvent(None,0,0,None)
        socket.setdefaulttimeout(60)

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.hWaitStop)

    def SvcDoRun(self):
        servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                              servicemanager.PYS_SERVICE_STARTED,
                              (self._svc_name_,''))
        self.main()

    def main(self):
        pass

if __name__ == '__main__':
    win32serviceutil.HandleCommandLine(AppServerSvc)
Run Code Online (Sandbox Code Playgroud)

您的代码将放在main()方法中 - 通常使用某种无限循环,可能会通过检查您在SvcStop方法中设置的标志来中断

  • @Kit:使用参数"install"从命令行运行脚本.然后,您将能够在Windows的"服务"列表中看到您的应用程序,您可以在其中启动它,停止它或将其设置为自动启动 (31认同)
  • 编码后,如何告诉Windows将其作为服务运行? (19认同)
  • 您特别提到pythoncom,并在示例代码中导入它.问题是你从未在示例代码中的任何地方实际使用pythoncom,只导入它.为什么要特别提及然后不显示它的用法? (16认同)
  • 为什么``socket.setdefaulttimeout(60)``是?它是否需要服务,还是只是从一些现有服务中意外复制?:) (10认同)
  • http://www.chrisumbel.com/article/windows_services_in_python这是一个类似的例子,但更完整 (6认同)
  • `python my_script_as_service.py install`? (3认同)
  • 我和@Timur在一起,`socket.setdefaulttimeout(60)`的原因是什么? (3认同)
  • 我使用这个代码,并且能够使用`python test_service.py install`安装服务但是当我尝试使用`python test_service.py start`或`NET START TestService`启动服务时,它返回一条消息"The服务没有响应控制功能",是否有必要在`SvcDoRun`方法中实现某些功能? (3认同)
  • 脚本顶部缺少"导入套接字".您还可以添加"_svc_description_"来更改服务的说明.谢谢这救了我的狩猎! (2认同)
  • @RicardoReyes那么你如何*卸载*这样的服务?如果在命令行帮助中看到``install`,`start`,`stop`和`remove`的`remove`,它们来自哪里,特别是它们是由pywin32发明的,还是......? (2认同)
  • 对于那些仍在挣扎的人。github要点上的这篇文章效果很好https://gist.github.com/guillaumevincent/d8d94a0a44a7ec13def7f96bfb713d3f (2认同)

mkn*_*naf 37

虽然几个星期前我对所选择的答案进行了投票,但与此同时我在这个主题上更加努力.感觉就像有一个特殊的Python安装和使用特殊模块来运行脚本作为服务是错误的方式.便携性等等呢?

我偶然发现了非常棒的非吸引服务管理器,这使得处理Windows服务非常简单明了.我想,既然我可以将选项传递给已安装的服务,我也可以选择我的Python可执行文件并将我的脚本作为选项传递.

我还没有尝试过这个解决方案,但我现在会这样做,并在整个过程中更新这篇文章.我也有兴趣在Windows上使用virtualenvs,所以我迟早会想出一个教程并在这里链接到它.

  • 这里的技巧是将python.exe作为服务运行,并将python脚本作为参数运行:如"nssm install MyServiceName c:\ python27\python.exe c:\ temp\myscript.py" (2认同)

pop*_*cnt 24

几乎任何Windows可执行文件都可以作为服务安装.

方法1:使用rktools.exe中的instsrv和srvany

对于Windows Home Server或Windows Server 2003(也适用于WinXP),Windows Server 2003资源工具包工具附带了可以串联使用的实用程序,称为instsrv.exesrvany.exe.有关如何使用这些工具的详细信息,请参阅此Microsoft知识库文章KB137890.

对于Windows Home Server,这些实用程序有一个很好的用户友好包装,名为aptly" Any Service Installer ".

方法2:使用ServiceInstaller for Windows NT

还有另一个使用ServiceInstaller for Windows NT的替代方法(可在此下载),并提供python指令.与名称相反,它也适用于Windows 2000和Windows XP.以下是有关如何将python脚本安装为服务的一些说明.

安装Python脚本

运行ServiceInstaller以创建新服务.(在这个例子中,假设python安装在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
Run Code Online (Sandbox Code Playgroud)

安装后,打开控制面板的服务小程序,选择并启动PythonTest服务.

在我最初的回答之后,我注意到已经在SO上发布了密切相关的问答.也可以看看:

我可以将Python脚本作为服务运行(在Windows中)吗?怎么样?

如何让Windows了解我用Python编写的服务?

  • 值得注意的是,我认为NT不一定与名称相反,至少在程序员的演讲中不是这样。它仅指“ NT *体系结构*”,而不是“ NT *品牌*”。也就是说,根据[Wikipedia对话](https://en.wikipedia.org/wiki/Talk:Windows_NT#Citation_for_claim_that_all_Windows_operating_systems_from_past_a_certain_date_are_Windows_NT.3F),这是有争议的,因为“这不是Microsoft的正式术语”,但是有尽管如此,这种思维方式还是一个传统。 (2认同)

pyO*_*ner 23

实现此目的的最简单方法是使用本机命令sc.exe:

sc create PythonApp binPath= "C:\Python34\Python.exe --C:\tmp\pythonscript.py"
Run Code Online (Sandbox Code Playgroud)
  1. https://technet.microsoft.com/en-us/library/cc990289(v=ws.11).aspx
  2. 使用sc.exe创建服务; 如何传入上下文参数

  • 这是行不通的。Windows 服务必须公开 pywin32 包所提供的特定接口。然而,普通的 Python 脚本是不够的。 (4认同)

小智 22

最简单的方法是使用:NSSM - 非吸吮服务管理器:

1 - 在https://nssm.cc/download上下载

2 - 将python程序安装为服务:以管理员身份启用Win提示符

c:> nssm.exe安装WinService

3 - 在NSSM的控制台上:

路径:C:\ Python27\Python27.exe

启动目录:C:\ Python27

参数:c:\ WinService.py

4 - 检查services.msc上创建的服务

  • 不要浪费更多时间并采用 NSSM 方法。对于虚拟环境,您只需指向 virtualenv 文件夹内的 python 可执行文件。 (6认同)
  • 注意:如果您的 *.py 脚本位于有空格的文件夹中(例如:C:\Program Files\myapp.py),需要在引号中指定参数:Arguments: "C:\Program Files\myapp.py" (4认同)
  • nssm 现在是一个死项目。最好使用 pywin32 方法 (3认同)

小智 13

逐步说明如何使其工作:

1-首先根据上面提到的基本骨架创建一个python文件.并将其保存到路径中,例如:"c:\ PythonFiles\AppServerSvc.py"

import win32serviceutil
import win32service
import win32event
import servicemanager
import socket


class AppServerSvc (win32serviceutil.ServiceFramework):
    _svc_name_ = "TestService"
    _svc_display_name_ = "Test Service"


    def __init__(self,args):
        win32serviceutil.ServiceFramework.__init__(self,args)
        self.hWaitStop = win32event.CreateEvent(None,0,0,None)
        socket.setdefaulttimeout(60)

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.hWaitStop)

    def SvcDoRun(self):
        servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                          servicemanager.PYS_SERVICE_STARTED,
                          (self._svc_name_,''))
        self.main()

    def main(self):
        # Your business logic or call to any class should be here
        # this time it creates a text.txt and writes Test Service in a daily manner 
        f = open('C:\\test.txt', 'a')
        rc = None
        while rc != win32event.WAIT_OBJECT_0:
            f.write('Test Service  \n')
            f.flush()
            # block for 24*60*60 seconds and wait for a stop event
            # it is used for a one-day loop
            rc = win32event.WaitForSingleObject(self.hWaitStop, 24 * 60 * 60 * 1000)
        f.write('shut down \n')
        f.close()

if __name__ == '__main__':
    win32serviceutil.HandleCommandLine(AppServerSvc)
Run Code Online (Sandbox Code Playgroud)

2 - 在此步骤中,我们应该注册我们的服务.

管理员身份运行命令提示符,输入:

sc create TestService binpath ="C:\ Python36\Python.exe c:\ PythonFiles\AppServerSvc.py"DisplayName ="TestService"start = auto

binpath的第一个参数是python.exe路径

binpath的第二个参数 是我们已经创建的python文件的路径

不要错过你应该在每个" = "符号后放一个空格.

如果一切正常,你应该看到

[SC] CreateService SUCCESS

现在你的python服务现在作为windows服务安装.您可以在Service Manager和注册表中看到它:

HKEY_LOCAL_MACHINE \系统\ CurrentControlSet \服务\ TestService的

3-现在好.您可以在服务管理器上启动服务.

您可以执行提供此服务框架的每个python文件.


小智 6

pysc:Python 上的服务控制管理器

作为从 pythonhosted.org 获取的服务运行的示例脚本:

from xmlrpc.server import SimpleXMLRPCServer

from pysc import event_stop


class TestServer:

    def echo(self, msg):
        return msg


if __name__ == '__main__':
    server = SimpleXMLRPCServer(('127.0.0.1', 9001))

    @event_stop
    def stop():
        server.server_close()

    server.register_instance(TestServer())
    server.serve_forever()
Run Code Online (Sandbox Code Playgroud)

创建并启动服务

import os
import sys
from xmlrpc.client import ServerProxy

import pysc


if __name__ == '__main__':
    service_name = 'test_xmlrpc_server'
    script_path = os.path.join(
        os.path.dirname(__file__), 'xmlrpc_server.py'
    )
    pysc.create(
        service_name=service_name,
        cmd=[sys.executable, script_path]
    )
    pysc.start(service_name)

    client = ServerProxy('http://127.0.0.1:9001')
    print(client.echo('test scm'))
Run Code Online (Sandbox Code Playgroud)

停止和删除服务

import pysc

service_name = 'test_xmlrpc_server'

pysc.stop(service_name)
pysc.delete(service_name)
Run Code Online (Sandbox Code Playgroud)
pip install pysc
Run Code Online (Sandbox Code Playgroud)

  • 有谁知道为什么这会被否决?它看起来是一个不错的解决方案。 (3认同)

fla*_*am3 5

我开始使用pywin32作为服务进行托管。

一切都很好,但我遇到了系统启动时服务无法在 30 秒(Windows 默认超时)内启动的问题。这对我来说至关重要,因为 Windows 启动是在一台物理机上托管的多个虚拟机上同时进行的,并且 IO 负载巨大。错误消息是:

Error 1053: The service did not respond to the start or control request in a timely fashion.

Error 7009: Timeout (30000 milliseconds) waiting for the <ServiceName> service to connect.

我与 pywin 进行了很多斗争,但最终还是使用了 NSSM,因为它是在这个答案中提出的。迁移到它非常容易。


cod*_*der 5

python中的nssm 3+

(我使用pyinstaller将我的 .py 文件转换为 .exe )

nssm:如前所述

  • 运行 nssm 安装 {ServiceName}
  • 在 NSSM 的控制台上:

    路径:path\to\your\program.exe

    启动目录:path\to\your\ #与路径相同但没有你的program.exe

    参数:空

如果您不想将项目转换为 .exe

  • 创建一个 .bat 文件 python {{your python.py file name}}
  • 并设置 .bat 文件的路径