0

我想通过python在windows服务中提示一个shell。以下是我的代码。它运行购买提示正在运行背景而不是显示。非常感谢!

import win32service
import win32serviceutil
import win32event 
import requests
import sys
import subprocess
import os

class PySvc(win32serviceutil.ServiceFramework):
    _svc_name_ = "ServicePython"  # NET START/STOP the service by the following name
    _svc_display_name_ = "ServicePython Service"  # name in the Service  Control Manager (SCM)
    _svc_description_ = "This service writes stuff to a file"  #  description in the SCM

    def __init__(self, args):
        win32serviceutil.ServiceFramework.__init__(self,args)
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)

    def SvcDoRun(self):
        import servicemanager   
        self.start()  
        win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)  

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) #tell the SCM 
        win32event.SetEvent(self.hWaitStop)         # fire the stop event
        self.stop()

    def start():
        pass  

    def stop():
        pass 

class MyService(PySvc): 
    def start(self):
        os.system("start cmd.exe /k \"cd /d C:\\ & dir\"") ####this does not show, runs at background only

if __name__ == '__main__':
    sys.argv.append("start")
    win32serviceutil.HandleCommandLine(MyService)
4

1 回答 1

0

一些我如何解决问题的方法。这是我的步骤:

  1. 使用 --interactive 选项安装 Windows 服务。
    如果上述python脚本的文件名是service.py,则通过cmd中的“python service.py --interactive install”进行安装。手动启动服务。

  2. 使用远程桌面连接会话 0。默认情况下
    ,窗口服务仅显示给会话 0 的用户。我按照链接连接到会话 0,现在可以看到提示 shell。

非常感谢。

于 2019-04-12T18:54:56.170 回答