0

i have a python program, this program will call a cmd(showTxt.exe) to do something,

when i run this python program in console, it works fine(the showTxt.exe works).

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

but when i run this program as a windows service, the showTxt.exe seems not work.

i have already save the dir path of showTxt.exe to environment path.

i've checked Event Logs, no errors or warnings

and change security setting of showTxt.exe not work.

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