当前位置:   article > 正文

Python实现Windows服务自启动、禁用、启动、停止、删除_python openscmanager

python openscmanager

如果一个程序被服务监管,那么仅仅kill程序是无用的,还要把服务关掉

设置自启动

import win32service
import win32serviceutil


def EnableService(service_name):
    try:
        # 获取服务管理器
        scm = win32service.OpenSCManager(None, None, win32service.SC_MANAGER_ALL_ACCESS)

        # 获取服务的句柄
        service = win32service.OpenService(scm, service_name, win32service.SERVICE_ALL_ACCESS)

        # 自启动服务
        win32service.ChangeServiceConfig(service, win32service.SERVICE_NO_CHANGE, win32service.SERVICE_AUTO_START, win32service.SERVICE_NO_CHANGE, None, None, 0, None, None, None, None)

        print(f"{service_name} service  enabled successfully.")
    except Exception as e:
        print(f"Failed to enable {service_name} service. Error: {e}")
    finally:
        # 关闭服务句柄
        win32service.CloseServiceHandle(service)
        # 关闭服务管理器句柄
        win32service.CloseServiceHandle(scm)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

启动服务

def StartService(service_name):
    try:
        win32serviceutil.StartService(service_name)
        print(f"{service_name} service started successfully.")
    except Exception as e:
        print(f"Failed to start {service_name} service. Error: {e}")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

停止服务

def StopService(service_name):
    try:
        # 获取服务管理器
        scm = win32service.OpenSCManager(None, None, win32service.SC_MANAGER_ALL_ACCESS)

        # 获取服务的句柄
        service = win32service.OpenService(scm, service_name, win32service.SERVICE_ALL_ACCESS)

        # 停止服务
        win32service.ControlService(service, win32service.SERVICE_CONTROL_STOP)

        print(f"{service_name} service  stopped successfully.")
    except Exception as e:
        print(f"Failed to stop {service_name} service. Error: {e}")
    finally:
        # 关闭服务句柄
        win32service.CloseServiceHandle(service)
        # 关闭服务管理器句柄
        win32service.CloseServiceHandle(scm)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

禁用服务

def DisableService(service_name):
    try:
        # 获取服务管理器
        scm = win32service.OpenSCManager(None, None, win32service.SC_MANAGER_ALL_ACCESS)

        # 获取服务的句柄
        service = win32service.OpenService(scm, service_name, win32service.SERVICE_ALL_ACCESS)

        # 禁用服务

        win32service.ChangeServiceConfig(service, win32service.SERVICE_NO_CHANGE, win32service.SERVICE_DISABLED, win32service.SERVICE_NO_CHANGE, None, None, 0, None, None, None, None)

        print(f"{service_name} service  Disabled successfully.")
    except Exception as e:
        print(f"Failed to disable {service_name} service. Error: {e}")
    finally:
        # 关闭服务句柄
        win32service.CloseServiceHandle(service)
        # 关闭服务管理器句柄
        win32service.CloseServiceHandle(scm)
def Remove_Service(service_name):
    try:
        win32serviceutil.RemoveService(service_name)
        print(f"Service '{service_name}' removed successfully.")
    except Exception as e:
        print(f"Failed to remove service '{service_name}'. Error: {e}")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

移除服务

import win32serviceutil

def RemoveService(service_name):
	try:
	    win32serviceutil.RemoveService(service_name)
	    print(f"Service '{service_name}' removed successfully.")
	except Exception as e:
	    print(f"Failed to remove service '{service_name}'. Error: {e}")

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
service_name = 'PostgreSQL_65432'
# EnableService(service_name)
StartService(service_name)
#StopService(service_name)
#DisableService(service_name)
#Remove_Service(service_name)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/71311
推荐阅读
相关标签
  

闽ICP备14008679号