Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python程序作为Windows服务_Python_Windows_Service - Fatal编程技术网

Python程序作为Windows服务

Python程序作为Windows服务,python,windows,service,Python,Windows,Service,下面是我试图将其转换为windows服务的代码。您将看到test.py作为它的调用,所有这些都是写入日志文件(作为测试)的短脚本 代码是用来让它成为windows服务的,它做得很好,但是当我运行它时,没有任何东西写入日志文件。非常感谢你的帮助。代码如下: import win32service import win32serviceutil import win32api import win32con import win32event import win32evtlogutil impor

下面是我试图将其转换为windows服务的代码。您将看到test.py作为它的调用,所有这些都是写入日志文件(作为测试)的短脚本

代码是用来让它成为windows服务的,它做得很好,但是当我运行它时,没有任何东西写入日志文件。非常感谢你的帮助。代码如下:

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

class aservice(win32serviceutil.ServiceFramework):

   _svc_name_ = "MyServiceShortName"
   _svc_display_name_ = "A python test"
   _svc_description_ = "Writing to a log"

   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 = 1000     #1 seconds
  # This is how long the service will wait to run / refresh itself (see script below)

  while 1:
     # Wait for service stop signal, if I 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("SomeShortNameVersion - STOPPED!")  #For Event Log
        break
     else:

              #what to run
              try:
                       file_path = "test.py"
                       execfile(file_path)
              except:
                       pass
             #end of what to run


def ctrlHandler(ctrlType):
   return True

if __name__ == '__main__':   
   win32api.SetConsoleCtrlHandler(ctrlHandler, True)   
   win32serviceutil.HandleCommandLine(aservice)
编辑:考虑到这一点,我应该包含test.py文件的代码,它有不必要的导入,但如果单独运行它,它将完成任务

import win32service
import win32serviceutil
import win32api
import win32con
import win32event
import win32evtlogutil
import os
logfile = open("log.txt", "a") #open file to log restart timestamp
logfile.write("\nthat's good!!!!")
logfile.close()

好的,所以我想出来了,我想回来发帖,以防其他人正在处理这个问题,尽管这有点独特

如果您在windows服务中,则必须指定文件路径,duh。。。但它没有完成,我无缘无故地把头发拔了出来

file_path = "test.py"
应该是

file_path = r"c:\users\...\test.py"

对windows文件路径使用“\”时要小心。它们必须像“\\”一样转义,或者字符串必须声明为原始字符串('r')。使用类似unix的斜杠“/”分隔符也可以工作,但对于windows用户来说可能看起来很奇怪。

当1:代码块时,它到底在哪里?它看起来像是班上最高级别的。这合法吗?您能再次检查缩进并确保一切正常吗?您是否尝试过指定日志文件的路径?我不确定服务“运行”在哪个目录。@andrewcooke是的,我试过几种不同的方法,甚至只是打个电话。例如,取出文件_pat=“test.py”,只放入一条打印语句或日志记录方法。@sarnold good catch,在这里发布时发生的缩进错误,在代码内部,这很好,您的权利,它应该是运行的一部分