【问题标题】:How can I run python service script like a running windows service?如何像运行 Windows 服务一样运行 python 服务脚本?
【发布时间】:2019-08-26 08:58:53
【问题描述】:

我使用 Pyinstaller 将我的 Python 脚本转换为 exe 格式。然后我编写了另一个 Python 代码来运行这个 exe,但我不想这样。我想用户单击 service.exe 并且程序像 Windows 服务一样运行。我的服务代码只是检查它是否正在运行的简单示例。我的 run.exe 文件有管理代码,我的意思是你必须使用“以管理员身份运行”选项运行它。但我也不想要这种方式。有没有办法解决这些问题?

我的服务代码:


import time
from pathlib import Path
import win32serviceutil


class Service(win32serviceutil.ServiceFramework):
    _svc_name_ = "Service"
    _svc_display_name_ = "Service"
    _svc_description_ = "a"

    def start(self):
        self.isrunning = True

    def stop(self):
        self.isrunning = False

    def main(self):


        while self.isrunning:

            Path(f'C:\\Users\\lenovo\\Desktop\\1.txt').touch()
            time.sleep(5)
    def parse_command_line(cls):

        win32serviceutil.HandleCommandLine(cls)

if __name__ == '__main__':
    Service.parse_command_line(Service)

我的运行服务代码:


import os 
path=os.getcwd()
command='"sc create MyService binpath="'+path+'\\Service.exe" start=auto"'
os.system(command)


【问题讨论】:

    标签: python service windows-services exe


    【解决方案1】:

    创建一个简单的批处理脚本来启动 python 脚本。

    例如:

    SET PATH=%PATH%;X:\PATH_TO_PYTHON
    python yourscript.py
    PAUSE
    

    “SET PATH...”只有在没有设置环境变量时才需要。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-11
      • 2016-06-26
      • 2010-09-07
      • 1970-01-01
      • 2013-05-01
      相关资源
      最近更新 更多