请教下如何刷新系统服务的状态
- Python code
for service in c.Win32_Service(): if (service.Name).find('dmserver')>-1: print service.State while service.State=='Running': print service.State os.system('net stop dmserver') time.sleep(10)
请教下 我现在使用这种方法能达到自动查找到我要的服务 并且关闭服务
但是那个while判断里 这么写虽然服务已经被关掉了 但while的 service.State状态没有变化 这么写就是永远循环了 请教下 代码刷新下服务状态怎么写?
[解决办法]
这个对象是在前面的代码创建的吧,所以状态还是前面获取到的。但也不能使用在循环中不断创建销毁对象这种暴力办法。wmi我也不太熟,我看看有没有刷新或者监控的方法。
[解决办法]
其实我也就看网站上的文章,你也可以看看:
http://timgolden.me.uk/python/wmi/tutorial.html
http://timgolden.me.uk/python/wmi/cookbook.html
wmi是啥你可以搜一下看,这东西内容也比较多,管理windows系统用的,也挺复杂。
[解决办法]
- Python code
#coding:utf-8import wmiimport timedef listservices(c): for service in c.Win32_Service(): print service.Caption,service.StartMode,service.Statedef watchservices(c): process_watcher=c.Win32_Process.watch_for("operation") while True: new_process=process_watcher() print new_process.Captiondef querystatus(c): wql="select name,state,status from Win32_Service where Name='MySQL51'" while True: print 'begin query service status:',time.ctime() for item in c.query(wql): print item if item.State.upper()=='STOPPED': return time.sleep(3) if __name__=='__main__': print 'begin' c = wmi.WMI() #listservices(c) querystatus(c) print 'end'
[解决办法]