将 Python 脚本转换为 Windows 服务的问题 [英] Problems with converting a Python script into a Windows service

查看:26
本文介绍了将 Python 脚本转换为 Windows 服务的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有一个连续运行的 python 脚本.它与此非常相似:https://github.com/walchko/Black-Hat-Python/blob/master/BHP-Code/Chapter10/file_monitor.py

I already have a python script that runs continuously. It's very similar to this: https://github.com/walchko/Black-Hat-Python/blob/master/BHP-Code/Chapter10/file_monitor.py

与 in 类似,当它作为脚本运行时,它会打开一个 CMD,当事情发生时显示一些数据 - 它不是用户可交互的,所以它不是强制性的(以防万一有人希望指出 Windows 服务不能有接口)

Similar as in, when running it as a script, it opens a CMD which shows some data when stuff happens - it's not user-interactible so it's not mandatory that it shows (just in case someone wishes to point out that windows services can't have interfaces)

我已尝试将其转换为服务.它开始几分之一秒,然后自动停止.当尝试通过 services.msc(而不是 python script.py start)启动它时,它根本不启动,Windows 错误提示如下:本地计算机上的服务已启动然后停止",这听起来像是发生了什么,如果我试着从论点开始.

I've tried to convert it to a service. It starts for a fraction of a second and then automatically stops. When trying to start it via services.msc (instead of python script.py start) it doesn't start at all, Windows error says something like: "The service on local computer started and then stopped" which sounds just about what's happening if I try to start it with the argument.

我尝试修改脚本以允许它作为服务运行 - 添加我在这里找到的骨架:是否可以在 Windows 中将 Python 脚本作为服务运行?如果可能,怎么做?

I've tried modifying the script to allow it to run as a service - adding the skeleton I found here: Is it possible to run a Python script as a service in Windows? If possible, how?

我也尝试过获取上面的骨架脚本,并尝试使用以下示例运行其他脚本:从另一个 Python 脚本调用 Python 脚本的最佳方法是什么?

I've also tried just getting the skeleton script above and just trying to make it run the other script with examples from here: What is the best way to call a Python script from another Python script?

有人知道将上面的脚本作为服务运行的最佳做法是什么吗?

Does anyone have any idea what the best course of action would be to run that script above as a service?

谢谢!

推荐答案

已编辑

"...服务可以在电脑开机时自动启动,可以暂停并重新启动,并且不显示任何用户界面." ~Windows 服务应用程序简介

Windows 服务需要实现才能使特定界面可用:

服务

因此您需要通过 Python 访问 Windows API:

您可以看到 示例代码,来自Win32 上的 Python 编程,其中第 18 章服务 (ch18_services文件夹)包含一个示例 (SmallestService.py),演示用 Python 编写的最小服务:

You can see the example code, from Python Programming On Win32, within which Chapter 18 Services (ch18_services folder) contains a sample (SmallestService.py) demonstrating the smallest possible service written in Python:

# SmallestService.py
# 
# A sample demonstrating the smallest possible service written in Python.

import win32serviceutil
import win32service
import win32event

class SmallestPythonService(win32serviceutil.ServiceFramework):
    _svc_name_ = "SmallestPythonService"
    _svc_display_name_ = "The smallest possible Python Service"
    def __init__(self, args):
        win32serviceutil.ServiceFramework.__init__(self, args)
        # Create an event which we will use to wait on.
        # The "service stop" request will set this event.
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)

    def SvcStop(self):
        # Before we do anything, tell the SCM we are starting the stop process.
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        # And set my event.
        win32event.SetEvent(self.hWaitStop)

    def SvcDoRun(self):
        # We do nothing other than wait to be stopped!
        win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)

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

<小时>

您可能需要在此处为​​您的特定 Python 环境下载适当的 pywin32 轮:http://www.lfd.uci.edu/~gohlke/pythonlibs/#pywin32

安装(系统范围内,从管理员 cmd 提示符):

And install it (system wide, from admin cmd prompt):

> cd \program files\python<ver>\scripts
> pip install \path\to\pywin32‑221‑cp<ver>‑cp<ver>m‑win_<arch>.whl

或安装它(每个用户,从常规 cmd 提示符):

Or install it (per user, from regular cmd prompt):

> cd \program files\python<ver>\scripts
> pip install --user \path\to\pywin32‑221‑cp<ver>‑cp<ver>m‑win_<arch>.whl

一定要适当地替换出现的.

这篇关于将 Python 脚本转换为 Windows 服务的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆