By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,301 Members | 1,752 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,301 IT Pros & Developers. It's quick & easy.

Check a windows service

P: n/a
Hi,

I'm new in python and I would like to know if it's possible to check if
a specific windows service is present and if it's possible how can I
do?

Thanks

Jan 16 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
awel wrote:
I'm new in python and I would like to know if it's possible to check if
a specific windows service is present and if it's possible how can I
do?
This is one way:

http://tgolden.sc.sabren.com/python/...matic_services

You'd have to change that example slightly, but I
hope the principal is clear enough. If not, ask again.

TJG

Jan 16 '07 #2

P: n/a
On 16 Jan 2007 09:09:34 -0800, Tim Golden <tj******@gmail.comwrote:
awel wrote:
I'm new in python and I would like to know if it's possible to check if
a specific windows service is present and if it's possible how can I
do?

This is one way:

http://tgolden.sc.sabren.com/python/...matic_services

You'd have to change that example slightly, but I
hope the principal is clear enough. If not, ask again.

TJG
Here's some code cut & pasted from the demos included in the win32 module:

import win32service
import win32con
def EnumServices():
resume = 0
accessSCM = win32con.GENERIC_READ
accessSrv = win32service.SC_MANAGER_ALL_ACCESS

#Open Service Control Manager
hscm = win32service.OpenSCManager(None, None, accessSCM)

#Enumerate Service Control Manager DB

typeFilter = win32service.SERVICE_WIN32
stateFilter = win32service.SERVICE_STATE_ALL

statuses = win32service.EnumServicesStatus(hscm, typeFilter, stateFilter)
for (short_name, desc, status) in statuses:
print short_name, desc, status
Jan 16 '07 #3

P: n/a
"awel" <al************@gmail.comescribió en el mensaje
news:11*********************@l53g2000cwa.googlegro ups.com...
I'm new in python and I would like to know if it's possible to check if
a specific windows service is present and if it's possible how can I
do?
Yes, using the wmi module, but you'll have to search the Microsoft
documentation on how to enumerate services and their properties:
http://msdn2.microsoft.com/en-us/library/aa384642.aspx
The code might look like this, but the names may be wrong:

import wmi
import pythoncom
pythoncom.CoInitialize()
w = wmi.WMI()
for s in w.Win32_Services():
print s

--
Gabriel Genellina
Jan 16 '07 #4

P: n/a
Sorry, but could you give me an example with a real service 'cause I've
tried this script but nothings happened, no error, nothings ; even if I
launch it in cmd prompt.

Thanks

Tim Golden a écrit :
awel wrote:
I'm new in python and I would like to know if it's possible to check if
a specific windows service is present and if it's possible how can I
do?

This is one way:

http://tgolden.sc.sabren.com/python/...matic_services

You'd have to change that example slightly, but I
hope the principal is clear enough. If not, ask again.

TJG
Jan 16 '07 #5

P: n/a
Sorry, but could you give me an example with a real service 'cause I've
tried this script but nothings happened, no error, nothings ; even if I
launch it in cmd prompt.

Thanks

Tim Golden a écrit :
awel wrote:
I'm new in python and I would like to know if it's possible to check if
a specific windows service is present and if it's possible how can I
do?

This is one way:

http://tgolden.sc.sabren.com/python/...matic_services

You'd have to change that example slightly, but I
hope the principal is clear enough. If not, ask again.

TJG
Jan 16 '07 #6

P: n/a
Sorry, but could you give me an example with a real service 'cause I've
tried this script but nothings happened, no error, nothings ; even if I
launch it in cmd prompt.

Thanks
Chris Mellon a écrit :
On 16 Jan 2007 09:09:34 -0800, Tim Golden <tj******@gmail.comwrote:
awel wrote:
I'm new in python and I would like to know if it's possible to check if
a specific windows service is present and if it's possible how can I
do?
This is one way:

http://tgolden.sc.sabren.com/python/...matic_services

You'd have to change that example slightly, but I
hope the principal is clear enough. If not, ask again.

TJG

Here's some code cut & pasted from the demos included in the win32 module:

import win32service
import win32con
def EnumServices():
resume = 0
accessSCM = win32con.GENERIC_READ
accessSrv = win32service.SC_MANAGER_ALL_ACCESS

#Open Service Control Manager
hscm = win32service.OpenSCManager(None, None, accessSCM)

#Enumerate Service Control Manager DB

typeFilter = win32service.SERVICE_WIN32
stateFilter = win32service.SERVICE_STATE_ALL

statuses = win32service.EnumServicesStatus(hscm, typeFilter, stateFilter)
for (short_name, desc, status) in statuses:
print short_name, desc, status
Jan 16 '07 #7

P: n/a
At Tuesday 16/1/2007 19:19, awel wrote:
>Sorry, but could you give me an example with a real service 'cause I've
tried this script but nothings happened, no error, nothings ; even if I
launch it in cmd prompt.
You have to call the EnumServices function if you want some useful output.
Copy&paste the code in a new file, add these 2 lines at the end, and
save the file with a .py extension:

if __name__=='__main__':
EnumServices()

Then execute it with: python your_file_name.py
See
http://diveintopython.org/getting_to...g_modules.html
to understand what's that; and read the whole book, it's a good
tutorial if you already know another language.
--
Gabriel Genellina
Softlab SRL


__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Jan 17 '07 #8

P: n/a
awel wrote:
Sorry, but could you give me an example with a real service 'cause I've
tried this script but nothings happened, no error, nothings ; even if I
launch it in cmd prompt.
Well, as that example said, it was designed to show
automatic services which are not running. If you don't
have any then nothing will show. I assumed you could
use this as a starting point.

To list all services try this:

<code>
import wmi

c = wmi.WMI ()
for service in c.Win32_Service ():
print service.Caption

</code>

To show a specific service:

<code>
import wmi

c = wmi.WMI ()
for service in c.Win32_Service (Caption="Task Scheduler"):
print service

</code>

TJG

Jan 17 '07 #9

P: n/a
Thanks for all 'cause you've really helped me

Just one thing in the last line for the specific service, you' ve
writted :
print service
but I think it is :
print service.Caption

Tim Golden a écrit :
awel wrote:
Sorry, but could you give me an example with a real service 'cause I've
tried this script but nothings happened, no error, nothings ; even if I
launch it in cmd prompt.

Well, as that example said, it was designed to show
automatic services which are not running. If you don't
have any then nothing will show. I assumed you could
use this as a starting point.

To list all services try this:

<code>
import wmi

c = wmi.WMI ()
for service in c.Win32_Service ():
print service.Caption

</code>

To show a specific service:

<code>
import wmi

c = wmi.WMI ()
for service in c.Win32_Service (Caption="Task Scheduler"):
print service

</code>

TJG
Jan 17 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.