• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            開源之路

            憶往昔, 項羽不過江. 江東好風(fēng)光! 今振臂一呼,率甲三千, 試問天!
            posts - 86, comments - 55, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            python 腳本作為Windows服務(wù)啟動

            Posted on 2007-12-25 15:12 江邊之鳥 閱讀(3885) 評論(0)  編輯 收藏 引用 所屬分類: python
            如果我們想讓系統(tǒng)啟動的時候就執(zhí)行某個程序,windows系統(tǒng)和unix系統(tǒng)是不一樣的,對于unix只需要將要執(zhí)行的命令放到 rc.local中,系統(tǒng)重新啟動的時候就可以加載了。windows就麻煩多了,如果你將程序放到啟動組中,只有輸入了密碼后,程序才被執(zhí)行,如果想在 系統(tǒng)一啟動的時候就執(zhí)行程序,必須使用nt服務(wù)。
            python下如何使用nt服務(wù),其實很簡單。
            下載python的win32支持。我使用的是:pywin32-202.win32-py2.3.exe安裝好后就可以來寫我們的服務(wù)了。
            我們先來建立一個空的服務(wù),建立test1.py這個文件,并寫入如下代碼:

            # -*- coding: cp936 -*-
            import win32serviceutil
            import win32service
            import win32event

            class test1(win32serviceutil.ServiceFramework):
                _svc_name_ = "test_python"
                _svc_display_name_ = "test_python"
                def __init__(self, args):
                    win32serviceutil.ServiceFramework.__init__(self, args)



                    self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)

                def SvcStop(self):
                    # 先告訴SCM停止這個過程
                    self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
                    # 設(shè)置事件
                    win32event.SetEvent(self.hWaitStop)

                def SvcDoRun(self):
                    # 等待服務(wù)被停止
                    win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)

            if __name__=='__main__':
                win32serviceutil.HandleCommandLine(test1)

            這里注意,如果你需要更改文件名,比如將win32serviceutil.HandleCommandLine(test1)中的test1更改為你的文件名,同時class也需要和你的文件名一致,否則會出現(xiàn)服務(wù)不能啟動的問題。
            在命令窗口執(zhí)行,test1.py可以看到幫助提示

            C:\>test1.py
            Usage: 'test1.py [options] install|update|remove|start [...]|stop|restart [...]|
            debug [...]'
            Options for 'install' and 'update' commands only:
             --username domain\username : The Username the service is to run under
             --password password : The password for the username
             --startup [manual|auto|disabled] : How the service starts, default = manual
             --interactive : Allow the service to interact with the desktop.

            C:\>

            安裝我們的服務(wù)

            [code:1:05b7353f1c]C:\>test1.py install
            Installing service test_python to Python class C:\test1.test1
            Service installed

            C:\>

            我們就可以用命令或者在控制面板-》管理工具-》服務(wù)中管理我們的服務(wù)了。在服務(wù)里面可以看到test_python這個服務(wù),雖然這個服務(wù)什么都不做,但能啟動和停止他。
            囯产极品美女高潮无套久久久| 99久久人人爽亚洲精品美女| 色诱久久av| 久久无码AV中文出轨人妻| 亚洲精品无码久久久久| segui久久国产精品| 久久久久久久波多野结衣高潮| 99久久中文字幕| 久久人人爽人人人人片av| 国产成人精品综合久久久| 久久婷婷成人综合色综合| 伊人久久精品影院| 国产女人aaa级久久久级| 久久精品夜夜夜夜夜久久| 欧美性大战久久久久久| 国产激情久久久久影院| 久久ZYZ资源站无码中文动漫 | 国产精品欧美亚洲韩国日本久久 | 亚洲欧美精品一区久久中文字幕| 熟妇人妻久久中文字幕| 亚洲精品久久久www| 狠狠精品干练久久久无码中文字幕| 久久精品无码一区二区无码| 久久精品国产2020| 综合网日日天干夜夜久久| 亚洲人成无码www久久久| 久久久久国产精品嫩草影院| 91精品观看91久久久久久| 青青青国产成人久久111网站| 久久精品国产99久久久| 久久A级毛片免费观看| 成人国内精品久久久久影院| 久久精品亚洲中文字幕无码麻豆 | 狠狠人妻久久久久久综合| 国产高潮久久免费观看| 国产精品免费久久久久影院| 国产精品成人久久久久三级午夜电影| 精品久久综合1区2区3区激情| 品成人欧美大片久久国产欧美| 国产精品成人精品久久久| 精品久久久久久无码专区 |