通过python在窗口服务中提示shell

问题描述 投票:0回答:1

我想通过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)
python cmd subprocess system win32serviceutil
1个回答
0
投票

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

  1. 使用--interactive选项安装Windows服务。 如果上面的python脚本的文件名是service.py,那么在cmd中通过“python service.py --interactive install”安装它。手动启动服务。
  2. 使用远程桌面连接到会话0。 窗口服务仅由default显示给会话0的用户。我按照link连接到会话0,现在可以看到提示shell。

非常感谢你。

© www.soinside.com 2019 - 2024. All rights reserved.