2008-08-29 54 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 
相關問題