python创建window service无法启动

发布于 2022-09-02 00:58:03 字数 1278 浏览 9 评论 0

服务可以安装,但是启动就自动停止,弹窗提示本地计算机上的HelloWorld Service启动后停止,某些服务在未由其他服务或程序使用时将自动停止,通过run.py运行没有问题,求解什么原因?

class HelloWorldSvc (win32serviceutil.ServiceFramework):
    _svc_name_ = "HelloWorld-Service"
    _svc_display_name_ = "HelloWorld Service"

    def __init__(self,args):
        win32serviceutil.ServiceFramework.__init__(self,args)
        self.stop_event = win32event.CreateEvent(None,0,0,None)
        socket.setdefaulttimeout(60)
        self.stop_requested = False

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.stop_event)
        logging.info('Stopping service ...')
        self.stop_requested = True

    def SvcDoRun(self):
        servicemanager.LogMsg(
            servicemanager.EVENTLOG_INFORMATION_TYPE,
            servicemanager.PYS_SERVICE_STARTED,
            (self._svc_name_, '')
        )
        self.main()

    @staticmethod
    def main():
        # 运行程序
        file_path = os.path.split(os.path.realpath(__file__))[0] + '\\run.py'
        print file_path
        try:
            execfile(file_path)
        except:
            pass


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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

写给空气的情书 2022-09-09 00:58:03

要不要试试在SvcDoRun方法里写个while循环,每次sleep个1秒,循环60次结束看看是不是service启动后1分钟才停止? 试试看呢。

有木有妳兜一样 2022-09-09 00:58:03
...
import inspect

this_file = inspect.getfile(inspect.currentframe())
dirpath = os.path.abspath(os.path.dirname(this_file))
sys.stdout = sys.stderr = open(os.path.join(dirpath, "service.log"), 'w')
...

https://stackoverflow.com/que...

み零 2022-09-09 00:58:03

同学你好!请问这个问题你解决了吗?我也碰到过这个问题

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文