2011-04-12 91 views
1

我创建了一个WINDWOS服务利用下面的代码窗口服务运行时:的Python脚本可以从命令行运行时,但没有从

import win32service 
import win32serviceutil 
import win32api 
import win32con 
import win32event 
import win32evtlogutil 
import os, sys, string, time 

class aservice(win32serviceutil.ServiceFramework): 

    _svc_name_ = "PAStoDistillerIFC" 
    _svc_display_name_ = "PAS DW to Distiller Interface" 
    _svc_description_ = "Service that checks the Clinical Research folder for any new files from PAS to process in Distiller" 

    def __init__(self, args): 
      win32serviceutil.ServiceFramework.__init__(self, args) 
      self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)   

    def SvcStop(self): 
      self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) 
      win32event.SetEvent(self.hWaitStop)      

    def SvcDoRun(self): 
     import servicemanager  
     servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,servicemanager.PYS_SERVICE_STARTED,(self._svc_name_, '')) 

     #self.timeout = 640000 #640 seconds/10 minutes (value is in milliseconds) 
     self.timeout = 120000  #120 seconds/2 minutes 
     # This is how long the service will wait to run/refresh itself (see script below) 

     while 1: 
     # Wait for service stop signal, if timeout, loop again 
     rc = win32event.WaitForSingleObject(self.hWaitStop, self.timeout) 
     # Check to see if self.hWaitStop happened 
     if rc == win32event.WAIT_OBJECT_0: 
      # Stop signal encountered 
      servicemanager.LogInfoMsg("PAStoDistillerIFC - STOPPED!") #For Event Log 
      break 
     else: 
       #[actual service code between rests] 
       try: 
        file_path = "D:\\SCRIPTS\\script.py" 
        execfile(file_path)    #Execute the script 
       except: 
        servicemanager.LogInfoMsg("File CRASHED") 
        pass 
       #[actual service code between rests] 


def ctrlHandler(ctrlType): 
    return True 

if __name__ == '__main__': 
    win32api.SetConsoleCtrlHandler(ctrlHandler, True) 
    win32serviceutil.HandleCommandLine(aservice) 

要运行此脚本:

import os, re, urllib, urllib2, time, datetime 
def postXML(path, fname): 
    fileresultop = open("D:\\CLinicalResearch\\SCRIPTS\\LOG.txt", 'a') # open result file 
    fileresultop.write('CheckXXX ') 
    fileresultop.close() 
    now = datetime.datetime.now() #####ALWAYS CRASHES HERE###### 
    fileresult = open("D:\\SCRIPTS\\IFCPYTHONLOG.txt", 'a') # open result file 
    fileresultop = open("D:\\SCRIPTS\\LOG.txt", 'a') 
    fileresultop.write('Check2 ') 
    fileresultop.close() 

path="D:\\Test2" # Put location of XML files here. 
procpath="D:\\Test2Processed" # Location of processed files 
now = datetime.datetime.now() 
dirList=os.listdir(path) 
for fname in dirList: # For each file in directory 
    if re.search("PatientIndexInsert", fname): # Brand new patient records 
       fileresultop = open("D:\\SCRIPTS\\LOG.txt", 'a') # open result file 
       fileresultop.write('Check1 ') 
       fileresultop.close() 
       postXML(path, fname) 

我有将脚本划分为裸码,我相信这会崩溃。

这从命令行完美工作,我在我自己的登录下运行Windows服务。 一旦我将datetime函数从函数中取出,它似乎就起作用了。

编辑1:我看到该服务在空白环境中运行。我自己没有任何环境变量。

编辑2:新增追踪:

File "D:\ClinicalResearch\SCRIPTS\PAS2DIST.py", line 23, in <module> 
    postXML(path, fname) 
File "D:\ClinicalResearch\SCRIPTS\PAS2DIST.py", line 6, in postXML 
    now = datetime.datetime.now() 
NameError: global name 'datetime' is not defined 
+0

你有没有追溯到坠机事件?您可以使用'traceback'模块将其作为字符串获取,以便在需要时进行记录。 – 2011-04-12 12:09:14

+0

如果它在日期时间崩溃,我会认为它必须处理配置文件的区域设置(或作为服务运行时没有上述设置)。在C++中会有'CreateProcessAsUser',也许有类似的python函数。 – 2011-04-12 12:13:48

+0

如果您在代码开始时导入win32traceutil,则可以在安装时使用win32traceutil.py(C:\ Python27 \ Lib \ site-packages \ win32 \ lib)来监视作为服务运行时的输出。这应该让你看到更好的错误。 – 2011-04-12 12:17:33

回答

0

我没有找到原因,但我没有找到一个解决办法。

我需要将所有相同的库导入到函数中。一旦我做到了,就像魅力一样。

希望这可以帮助别人。

相关问题