Skip to content

Instantly share code, notes, and snippets.

@joncutrer
Last active September 27, 2023 09:08
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save joncutrer/905f73c75d8bb12fe1d714148b58d74b to your computer and use it in GitHub Desktop.
Save joncutrer/905f73c75d8bb12fe1d714148b58d74b to your computer and use it in GitHub Desktop.
Python: Check if Windows Service is running|installed without pywin32
import psutil
#
# Python Script to Check if...
# Windows Service is found|installed,stopped|running without pywin32
# Found at https://stackoverflow.com/questions/33843024
# Update to work on python 3.x
#
def getService(name):
service = None
try:
service = psutil.win_service_get(name)
service = service.as_dict()
except Exception as ex:
print(str(ex))
return service
service = getService('LanmanServer')
print(service)
if service:
print("service found")
else:
print("service not found")
if service and service['status'] == 'running':
print("service is running")
else:
print("service is not running")
@Damle33
Copy link

Damle33 commented Jun 18, 2020

Hi, I need little help with this code, I need to create a loop so that it will check the service after every 30 sec and if service is down it will restart that service can you help me with that.

@hehehe886
Copy link

Hi, I need little help with this code, I need to create a loop so that it will check the service after every 30 sec and if service is down it will restart that service can you help me with that.

actually i been do this script.Use thread for loop suggest in advance 😂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment