Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python restarting windows services

Tags:

python

Quick question:

I have a python script that will restart a windows service:

import os 

import win32serviceutil

serviceName = "Apple Mobile Device"


win32serviceutil.StopService(serviceName)

I need to add several other services.

How would I do that?

Thx

like image 268
zooted Avatar asked Feb 19 '15 21:02

zooted


1 Answers

If you want to manually enter them each time do something like this:

import os 
import win32serviceutil

stopping = 1
while stopping == 1:
    service_name = raw_input('enter the name of the service[s] you would like to stop\nor enter done or exit to exit\n: ')
    if service_name.lower() == 'done' or 'exit':
        stopping = 2
    else:
        try:
            win32serviceutil.StopService(service_name)
            print '{} stopped'.format(service_name)
        except:
            print 'could not stop service {}'.format(service_name)

elif you want to do it automagically every time you run the function do something more like paidhima:

import wmi
import os 
import win32serviceutil



service_name = ['this is service one  :)'
                ,'service 2'
                ,'service 1 million'
                ]
for s in service_name:
    win32serviceutil.StopService(s)

else you could list all the services and crash your machine by shutting them all down :)

like image 146
Yojimbo Avatar answered Sep 20 '22 14:09

Yojimbo