【问题标题】:environment path not work in windows service环境路径在 Windows 服务中不起作用
【发布时间】:2012-10-12 06:48:32
【问题描述】:

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

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

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

但是当我以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)

【问题讨论】:

    标签: python windows-services windows-server-2003


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2015-08-18
      • 2011-02-02
      • 2016-05-06
      • 2014-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-15
      • 1970-01-01
      相关资源
      最近更新 更多