在another question我昨天發佈了一篇關於Python腳本如何作爲Windows服務運行的非常好的建議。我還想知道的是:Windows如何意識到可以在本地工具中管理的服務(「管理工具」中的「服務」窗口)。 I. e。在Linux下,在/etc/init.d中將啓動/停止腳本放在什麼位置等同於Windows?如何讓Windows意識到我用Python編寫的服務?
回答
與Windows中最「意識到」的東西,答案是「註冊表」。
看看這Microsoft知識庫文章在:http://support.microsoft.com/kb/103000
搜索「可以由服務控制器啓動的並且服從服務控制協議Win32程序。」這是您感興趣的服務類型。
服務註冊(KEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Services的內容爲 \ myservice)包含有關服務的信息,包括其可執行位置等信息,它會失敗(停止操作系統?),必須在此之前啓動哪些服務,它運行的是哪個用戶。
對於服務控制協議,您的程序的main()應該調用Windows API調用,爲您的服務設置啓動,停止和暫停的回調。你在這些回調中所做的全部取決於你。
這裏是代碼安裝Python的腳本作爲一種服務,用Python編寫的:)
http://code.activestate.com/recipes/551780/
這篇文章也可以幫助你:
http://essiene.blogspot.com/2005/04/python-windows-services.html
可以使用SRVANY.EXE從Windows NT資源工具包來創建用戶定義的服務,將在管理工具中顯示...
http://support.microsoft.com/kb/137890
我使用這種方法來運行tracd( trac的一個python腳本/服務器)。
這裏有一些非常明確的指示:http://www.tacktech.com/display.cfm?ttid=197
它確實需要一些註冊表編輯(非常小的,容易),但將允許你進行任何命令行/腳本Windows服務。
不要與註冊表直接混淆。用戶使用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
- 1. 如何獲取用C#編寫的Windows服務中的ServiceName?
- 2. 我如何編寫一個Windows服務來管理其他的Windows服務?(我能嗎?)
- 3. 如何調試在VS2008中用VC++編寫的Windows服務?
- 4. 如何編寫jPOS服務
- 5. 如何讓ASP.NET識別我的自定義服務器控件?
- 6. 如何讓HSQLDB成爲Windows服務?
- 7. 如何讓WiX刪除Windows服務?
- 8. 如何使用soap編寫web服務
- 9. 如何使用CPython編寫服務?
- 10. 如何使用cakephp編寫Web服務
- 11. 索賠意識WCF服務
- 12. 如何讓我的Windows服務以特定間隔運行?
- 13. 如何讓我的本地服務器區分大小寫?
- 14. 如何讓ipython意識到對自寫模塊所做的更改?
- 15. 如何讓我的應用程序服務器CPU達到100%?
- 16. 如何爲Windows服務程序編寫OnStart函數?
- 17. windows server 2008:如何識別誰登錄到服務器
- 18. 如何讓python識別我正在使用的窗口?
- 19. 用Java編寫的OPC服務器讓我的Java UI停止工作
- 20. 如何使用python從localhost連接到windows azure mysql服務器?
- 21. 如何使用C#.NET中的Windows服務調用Windows服務?
- 22. 如何讓用戶在我的服務器上運行任意源代碼
- 23. Windows服務意外終止
- 24. 如何查看依賴於Python服務的Windows服務?
- 25. 由C#編寫的Windows服務使用的類庫程序集#
- 26. 如何安排我的Windows服務?
- 27. 如何讓itemtemplate意識到它的包含模板?
- 28. Windows服務輸出寫入到文件
- 29. 如何編寫python HTTP服務器以偵聽多個端口?
- 30. Alexa沒有意識到我的意圖
註冊表黑客是不良形式。其實,可怕的形式。 .py應接受-install cmd行參數並將其自身安裝爲服務,使用已發佈的API和方法(例如,不通過直接註冊表訪問) – Jonesome 2013-07-24 22:58:37