Python command to stop and start windows services ?

Viewed 9015

what is the python command to stop and start windows services.I can't use win32serviceutil because am using latest python version 3.6.

3 Answers

The existing answer is very unpythonic and not simple enough.

I was searching for a Pythonic solution and stumbled upon this question.

I have a much simpler solution that isn't Pythonic.

Just use net start servicename with os.system.

For example, if we want to start MySQL80:

import os
os.system('net start MySQL80')

Now using it as a function:

import os

def start_service(svc):
    os.system(f'net start {svc}')

And to stop the service, use net stop servicename:

import os

def stop_service(svc):
    os.system(f'net stop {svc}')

I know my solution isn't Pythonic but the existing answer also isn't Pythonic and so far in my Google searching I haven't found anything both relevant and Pythonic.

Related