尝试创建一个 python 脚本来对服务进行定期检查。如果它没有运行,它将自动启动它。这是代码:
import win32serviceutil
from sched import scheduler
from time import time, sleep
s = scheduler(time, sleep)
def run_periodically(start, end, interval, func):
event_time = start
while event_time < end:
s.enterabs(event_time, 0, func, ())
event_time += interval
s.run()
if __name__ == '__main__':
machine = 'George'
service = 'Hamachi2Svc'
action = 'start'
def service_info(action, machine, service):
if win32serviceutil.QueryServiceStatus(service, machine)[1] == 4:
print "%s is currently running" % service
else:
print "%s is *not* running" % service
print "%s is starting..." % service
win32serviceutil.StartService(service, machine)
print '%s started successfully' % service
run_periodically(time()+5, time()+10, 1, service_info(action, machine, service))
这是产生的错误:
Traceback (most recent call last):
File "C:\Python27\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 323, in RunScript
debugger.run(codeObject, __main__.__dict__, start_stepping=0)
File "C:\Python27\Lib\site-packages\pythonwin\pywin\debugger\__init__.py", line 60, in run
_GetCurrentDebugger().run(cmd, globals,locals, start_stepping)
File "C:\Python27\Lib\site-packages\pythonwin\pywin\debugger\debugger.py", line 655, in run
exec cmd in globals, locals
File "C:\Users\Administrator\Desktop\HamachiDestroyerV.01.py", line 1, in <module>
import win32serviceutil
File "C:\Users\Administrator\Desktop\HamachiDestroyerV.01.py", line 13, in run_periodically
s.run()
File "C:\Python27\lib\sched.py", line 117, in run
action(*argument)
TypeError: 'NoneType' object is not callable
我想知道是否有人可以提供他们的专业知识并告诉我我做错了什么。我仍然是 python 的初学者,但这不是我制作的第一个程序。
请您参考如下方法:
您注册了 service_info() 的返回值;该函数返回 None。
如果您希望调度程序调用 service_info(),则需要注册对函数本身的引用以及参数元组:
run_periodically(time()+5, time()+10, 1, service_info, (action, machine, service))
并调整 run_periodically() 以接受参数并将其传递给 scheduler.enterabs() 方法:
def run_periodically(start, end, interval, func, args=()):
event_time = start
while event_time < end:
s.enterabs(event_time, 0, func, args)
event_time += interval
s.run()
