0

我有一个 python 程序,这个程序会调用一个 cmd(showTxt.exe) 来做一些事情,

当我运行这个 python 程序in console时,它工作正常(showTxt.exe 工作)。

p = Tarser(self.conf_parser)
p.parse()

但是当我将这个程序作为.exe 运行时windows service,showTxt.exe 似乎不起作用。

我已经将 showTxt.exe 的 dir 路径保存到environment path.

我检查过Event Logs,没有错误或警告

并更改 showTxt.exe 的安全设置不起作用。

class MyService(win32serviceutil.ServiceFramework):
    """Windows Service."""
    os.chdir(os.path.dirname(__file__))
    ini = "tarser_service.ini"
    conf_parser = ConfigParser.SafeConfigParser()
    conf_parser.read(ini)
    _svc_name_, _svc_display_name_, _svc_description_ = get_win_service(conf_parser)

    def __init__(self, args):
        if os.path.dirname(__file__):
            os.chdir(os.path.dirname(__file__))
        win32serviceutil.ServiceFramework.__init__(self, args)

        # create an event that SvcDoRun can wait on and SvcStop can set.
        self.stop_event = win32event.CreateEvent(None, 0, 0, None)

    def SvcDoRun(self):
        self.Run()
        win32event.WaitForSingleObject(self.stop_event, win32event.INFINITE)

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.stop_event)
        LoggerInstance.log("Tarser service is stopped")
        self.ReportServiceStatus(win32service.SERVICE_STOPPED)
        sys.exit()

    def Run(self):
        LoggerInstance.log("Tarser service is running, configuration: %s" %(self.ini,))
        if ((not self.conf_parser.has_section('Tsc')) or
            (not self.conf_parser.has_option('Tsc', 'check_interval')) or
            (not self.conf_parser.has_option('Tsc', 'running_time'))):
            LoggerInstance.log('Tarser service: no Tsc service parameters')
            self.SvcStop()

        # set configuration parameters from ini configuration
        self.check_interval = self.conf_parser.getint('Tsc', 'check_interval')
        running_time = self.conf_parser.get('Tsc', 'running_time')
        TscCheck_time = time.strptime(running_time, "%H %M")

        while 1:
            curr_time = time.gmtime()
            if curr_time.tm_hour == TscCheck_time.tm_hour and curr_time.tm_min == TscCheck_time.tm_min:
                p = Tarser(self.conf_parser)
                p.parse()
            #LoggerInstance.log("Tarser service: sleep %d seconds" %self.check_interval)
            time.sleep(self.check_interval)
4

1 回答 1

0

将 showTxt.exe 的目录路径添加到系统环境 PATH,而不是用户的环境 PATH。

于 2012-10-12T08:59:55.013 回答