Python 可执行文件作为 Windows 服务

标签 python python-2.7 batch-file service windows-7

我知道 StackOverflow 上有类似的主题,但没有一个与我有相同的问题。大多数问题都是询问如何从 Python 启动服务。我有一个正在创建服务的 .bat 文件,并使用我使用 py2exe 创建的 PythonFile.exe。我收到错误“启动服务时出错。服务未及时响应启动或控制请求”。该服务未运行,但我在 ProcessManager 进程中看到可执行文件。

可执行文件是否有资格成为服务?我的可执行文件只是一个 TCP 服务器,它会休眠(使用互斥体)直到互斥体解锁。

我的.bat 文件...

net stop "FabulousAndOutrageousOinkers"
%SYSTEMROOT%\system32\sc.exe delete "FabulousAndOutrageousOinkers"
%SYSTEMROOT%\system32\sc.exe create "FabulousAndOutrageousOinkers" binPath= "%CD%\FabulousAndOutrageousOinkers.exe" start= auto
net start "FabulousAndOutrageousOinkers"

最佳答案

我最终找到了我的问题的答案。事实上,成为一项服务是有要求的。大多数成为服务的脚本或程序在代码之上都有一个包装层来管理这些需求的处理。该包装器最终调用开发人员的代码,并用不同类型的状态向 Windows 服务发出信号。开始、停止等...

import win32service  
import win32serviceutil  
import win32event  

class Service(win32serviceutil.ServiceFramework):  
    # you can NET START/STOP the service by the following name  
    _svc_name_ = "FabulousAndOutrageousOinkers"  
    # this text shows up as the service name in the Service  
    # Control Manager (SCM)  
    _svc_display_name_ = "Fabulous And Outrageous Oinkers"  
    # this text shows up as the description in the SCM  
    _svc_description_ = "Truly truly outrageous"  

    def __init__(self, args):  
        win32serviceutil.ServiceFramework.__init__(self,args)  
        # create an event to listen for stop requests on  
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)  

    # core logic of the service     
    def SvcDoRun(self):  
        import servicemanager
        self.ReportServiceStatus(win32service.SERVICE_START_PENDING)  

        self.start() 
        rc = None  

        # if the stop event hasn't been fired keep looping  
        while rc != win32event.WAIT_OBJECT_0:  
            # block for 5 seconds and listen for a stop event  
            rc = win32event.WaitForSingleObject(self.hWaitStop, 5000)

        self.stop()

    # called when we're being shut down      
    def SvcStop(self):  
        # tell the SCM we're shutting down  
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)  
        # fire the stop event  
        win32event.SetEvent(self.hWaitStop)

    def start(self):
       try:
          file_path = "FabulousAndOutrageousOinkers.exe"
          execfile(file_path)             #Execute the script
       except:
          pass

    def stop(self):
      pass

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

我从http://www.chrisumbel.com/article/windows_services_in_python找到了这个模板。这段代码仍然存在一些问题,因为我收到错误“启动服务时出错:服务没有及时响应启动或控制请求”,但它仍然回答了我的问题。事实上,可执行文件要成为 Windows 服务是有要求的。

关于Python 可执行文件作为 Windows 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36484101/

相关文章:

python - Pandas pythonic 方式从 pd.Series 中提取值和索引作为元组

javascript - 如何使用递归遍历嵌套数组?

python - 如何在 Python 中只处理一次重复的有序列表中的每个项目?

javascript - 如何使用 Canvas 元素显示图形

python - 找到双线;更快的方法

python - @ 符号在 iPython/Python 中的作用是什么

python - python 中的访问被拒绝

powershell - 如何将参数从批处理文件传递到 Powershell 脚本内的函数

windows - 批处理文件跳过 If 语句

windows - 使用 echo|set/p= 时在变量中转义双引号