如何让 Windows 知道我用 Python 编写的服务?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I make Windows aware of a service I have written in Python?
提问by Hanno Fietz
In another questionI posted yesterday, I got very good advice on how a Python script could be run as a service in Windows. What I'm left wondering is: How is Windows aware of the services that can be managed in the native tools ("services" window in "administrative tools"). I. e. what is the Windows equivalent of putting a start/stop script in /etc/init.d under Linux?
在我昨天发布的另一个问题中,我得到了关于如何在 Windows 中将 Python 脚本作为服务运行的非常好的建议。我想知道的是:Windows 如何知道可以在本机工具中管理的服务(“管理工具”中的“服务”窗口)。IE。在 Linux 下将启动/停止脚本放在 /etc/init.d 中的 Windows 等价物是什么?
采纳答案by Espo
As with most "aware" things in Windows, the answer is "Registry".
与 Windows 中的大多数“感知”事物一样,答案是“注册表”。
Take a look at this Microsoft Knowledge Base article: http://support.microsoft.com/kb/103000
看看这篇 Microsoft 知识库文章:http: //support.microsoft.com/kb/103000
Search for "A Win32 program that can be started by the Service Controller and that obeys the service control protocol." This is the kind of service you're interested in.
搜索“可以由服务控制器启动并遵守服务控制协议的 Win32 程序”。这是您感兴趣的服务类型。
The service registration (contents of KEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services \myservice) carries information about the service, including things like its executable location, what to do when it fails (halt the OS?), what services must be started before this one, what user it runs as.
服务注册(KEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\myservice 的内容)携带有关服务的信息,包括它的可执行位置、失败时该怎么办(停止操作系统?)、在此之前必须启动哪些服务,它以什么用户身份运行。
As to service control protocol, main() of your program is supposed to invoke a Windows API call, setting up callbacks for start, stop, pause for your service. What you do in those callbacks is all up to you.
至于服务控制协议,您程序的 main() 应该调用 Windows API 调用,为您的服务设置启动、停止、暂停回调。你在这些回调中做什么完全取决于你。
回答by Ferruccio
Don't muck with the registry directly. User the SC command-line tool. Namely, SC CREATE
不要直接修改注册表。使用 SC 命令行工具。即,SC CREATE
DESCRIPTION: SC is a command line program used for communicating with the NT Service Controller and services. USAGE: sc [command] [service name] ... The option has the form "\ServerName" Further help on commands can be obtained by typing: "sc [command]" Commands: query-----------Queries the status for a service, or enumerates the status for types of services. queryex---------Queries the extended status for a service, or enumerates the status for types of services. start-----------Starts a service. pause-----------Sends a PAUSE control request to a service. interrogate-----Sends an INTERROGATE control request to a service. continue--------Sends a CONTINUE control request to a service. stop------------Sends a STOP request to a service. config----------Changes the configuration of a service (persistant). description-----Changes the description of a service. failure---------Changes the actions taken by a service upon failure. qc--------------Queries the configuration information for a service. qdescription----Queries the description for a service. qfailure--------Queries the actions taken by a service upon failure. delete----------Deletes a service (from the registry). create----------Creates a service. (adds it to the registry). control---------Sends a control to a service. sdshow----------Displays a service's security descriptor. sdset-----------Sets a service's security descriptor. GetDisplayName--Gets the DisplayName for a service. GetKeyName------Gets the ServiceKeyName for a service. EnumDepend------Enumerates Service Dependencies. The following commands don't require a service name: sc boot------------(ok | bad) Indicates whether the last boot should be saved as the last-known-good boot configuration Lock------------Locks the Service Database QueryLock-------Queries the LockStatus for the SCManager Database EXAMPLE: sc start MyService
回答by Espo
Here is code to install a python-script as a service, written in python :)
这是将 python 脚本安装为服务的代码,用 python 编写:)
http://code.activestate.com/recipes/551780/
http://code.activestate.com/recipes/551780/
This post could also help you out:
这篇文章也可以帮助你:
http://essiene.blogspot.com/2005/04/python-windows-services.html
http://essiene.blogspot.com/2005/04/python-windows-services.html
回答by mmattax
You can use srvany.exe from Windows NT Resource Kit to create a user defined service that will show up in the admin tools...
您可以使用 Windows NT Resource Kit 中的 srvany.exe 创建用户定义的服务,该服务将显示在管理工具中...
http://support.microsoft.com/kb/137890
http://support.microsoft.com/kb/137890
I am using this method to run tracd (a python script / server) for trac.
我正在使用这种方法为 trac 运行 tracd(一个 python 脚本/服务器)。
Here are some very clear instructions: http://www.tacktech.com/display.cfm?ttid=197
这里有一些非常明确的说明:http: //www.tacktech.com/display.cfm?ttid=197
It does require some registry editing (very minimal and easy) but will allow you to make any command line / script a windows service.
它确实需要一些注册表编辑(非常简单且简单),但允许您将任何命令行/脚本设为 Windows 服务。