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

如果一个程序被服务监管,那么仅仅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)

启动服务

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}")

停止服务

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)

禁用服务

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}")

移除服务

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}")

service_name = 'PostgreSQL_65432'
# EnableService(service_name)
StartService(service_name)
#StopService(service_name)
#DisableService(service_name)
#Remove_Service(service_name)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值