2008-08-29 52 views
10

another question我昨天发布了一篇关于Python脚本如何作为Windows服务运行的非常好的建议。我还想知道的是:Windows如何意识到可以在本地工具中管理的服务(“管理工具”中的“服务”窗口)。 I. e。在Linux下,在/etc/init.d中将启动/停止脚本放在什么位置等同于Windows?如何让Windows意识到我用Python编写的服务?

回答

3

与Windows中最“意识到”的东西,答案是“注册表”。

看看这Microsoft知识库文章在:http://support.microsoft.com/kb/103000

搜索“可以由服务控制器启动的并且服从服务控制协议Win32程序。”这是您感兴趣的服务类型。

服务注册(KEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Services的内容为 \ myservice)包含有关服务的信息,包括其可执行位置等信息,它会失败(停止操作系统?),必须在此之前启动哪些服务,它运行的是哪个用户。

对于服务控制协议,您的程序的main()应该调用Windows API调用,为您的服务设置启动,停止和暂停的回调。你在这些回调中所做的全部取决于你。

+0

注册表黑客是不良形式。其实,可怕的形式。 .py应接受-install cmd行参数并将其自身安装为服务,使用已发布的API和方法(例如,不通过直接注册表访问) – Jonesome 2013-07-24 22:58:37

0

可以使用SRVANY.EXE从Windows NT资源工具包来创建用户定义的服务,将在管理工具中显示...

http://support.microsoft.com/kb/137890

我使用这种方法来运行tracd( trac的一个python脚本/服务器)。

这里有一些非常明确的指示:http://www.tacktech.com/display.cfm?ttid=197

它确实需要一些注册表编辑(非常小的,容易),但将允许你进行任何命令行/脚本Windows服务。

7

不要与注册表直接混淆。用户使用SC命令行工具。即SC创建

 
    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 
相关问题