我有一个python程序,这个程序会调用cmd(showTxt.exe)来做某事,
当我运行这个python程序in console
时,它工作正常(showTxt.exe工作)。
p = Tarser(self.conf_parser)
p.parse()
但是当我以windows service
运行此程序时,showTxt.exe似乎不起作用。
我已经将showTxt.exe的目录路径保存到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)
答案 0 :(得分:0)
将showTxt.exe的目录路径添加到系统环境路径,而不是用户的环境路径。