用于停止Tomcat服务错误的Python脚本指定的服务不作为已安装的服务存在

时间:2016-01-26 11:29:41

标签: python-2.7

我正在使用Python脚本来停止Tomcat服务。我收到了错误:

error: (1060, 'GetServiceKeyName', 'The specified service does not exist as an installed service.')

停止服务的方法是:

def service_info(action, machine, service):
if action == 'stop':
    win32serviceutil.StopService(service, machine)
    print '%s stopped successfully' % service
elif action == 'start':
    win32serviceutil.StartService(service, machine)
    print '%s started successfully' % service
elif action == 'restart':
    win32serviceutil.RestartService(service, machine)
    print '%s restarted successfully' % service
elif action == 'status':
    if win32serviceutil.QueryServiceStatus(service, machine)[1] == 4:
        print "%s is running normally" % service
    else:
        print "%s is *not* running" % service

从我的基类unittest我调用这样的方法:

from Utilities.HelperMethods import recursive_overwrite, rename_file, delete_a_file, stop_a_service, service_info
class BaseTestCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls):

    machine = 'riaz-pc'
    service_tomcat = 'Tomcat7.exe'
    action_stop = 'stop'
    action_start = 'start'        
    service_info(action_stop, machine, service_tomcat)
    service_info(action_start, machine, service_tomcat)

我想我正在提供Tomcat的正确服务名称。在命令提示符CMD中我输入tasklist,我可以看到Tomcat7.exe 我将Tomcat7.exe作为服务名称 为什么会失败?

完整的错误日志是:

Traceback (most recent call last):
  File "C:\Webdriver\ClearCore 501\Base\BaseTestCase.py", line 35, in setUpClass
    service_info(action_stop, machine, service_tomcat)
  File "C:\Webdriver\ClearCore 501\Utilities\HelperMethods.py", line 134, in service_info
    win32serviceutil.StopService(service, machine)
  File "C:\Python27\lib\site-packages\win32\lib\win32serviceutil.py", line 409, in StopService
    return ControlService(serviceName, win32service.SERVICE_CONTROL_STOP, machine)
  File "C:\Python27\lib\site-packages\win32\lib\win32serviceutil.py", line 318, in ControlService
    hs = SmartOpenService(hscm, serviceName, win32service.SERVICE_ALL_ACCESS)
  File "C:\Python27\lib\site-packages\win32\lib\win32serviceutil.py", line 80, in SmartOpenService
    name = win32service.GetServiceKeyName(hscm, name)
error: (1060, 'GetServiceKeyName', 'The specified service does not exist as an installed service.')

谢谢, 里亚兹

1 个答案:

答案 0 :(得分:1)

我觉得服务名称错了。请尝试使用tomcat7代替Tomcat7.exe

win32serviceutil.StopService('tomcat7', 'riaz-pc')