Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python command to stop and start windows services ?

Tags:

python-3.x

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

like image 595
shane Avatar asked Oct 05 '17 22:10

shane


2 Answers

You could use the sc command line interface provided by Windows:

import subprocess
# start the service
args = ['sc', 'start', 'Service Name']
result = subprocess.run(args)
# stop the service
args[1] = 'stop'
result = subprocess.run(args)
like image 192
Bernhard Avatar answered Dec 26 '22 16:12

Bernhard


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.

like image 40
Thyebri Avatar answered Dec 26 '22 15:12

Thyebri