Check a windows service

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • awel

    #1

    Check a windows service

    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

  • Tim Golden

    #2
    Re: Check a windows service

    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:



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

    TJG

    Comment

    • Chris Mellon

      #3
      Re: Check a windows service

      On 16 Jan 2007 09:09:34 -0800, Tim Golden <tjgolden@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:
      >

      >
      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.GENERI C_READ
      accessSrv = win32service.SC _MANAGER_ALL_AC CESS

      #Open Service Control Manager
      hscm = win32service.Op enSCManager(Non e, None, accessSCM)

      #Enumerate Service Control Manager DB

      typeFilter = win32service.SE RVICE_WIN32
      stateFilter = win32service.SE RVICE_STATE_ALL

      statuses = win32service.En umServicesStatu s(hscm, typeFilter, stateFilter)
      for (short_name, desc, status) in statuses:
      print short_name, desc, status

      Comment

      • Gabriel Genellina

        #4
        Re: Check a windows service

        "awel" <albert.wellens @gmail.comescri bió en el mensaje
        news:1168966952 .965088.17740@l 53g2000cwa.goog legroups.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.CoIni tialize()
        w = wmi.WMI()
        for s in w.Win32_Service s():
        print s

        --
        Gabriel Genellina


        Comment

        • awel

          #5
          Re: Check a windows service

          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:
          >

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

          TJG

          Comment

          • awel

            #6
            Re: Check a windows service

            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:
            >

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

            TJG

            Comment

            • awel

              #7
              Re: Check a windows service

              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 <tjgolden@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:



              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.GENERI C_READ
              accessSrv = win32service.SC _MANAGER_ALL_AC CESS
              >
              #Open Service Control Manager
              hscm = win32service.Op enSCManager(Non e, None, accessSCM)
              >
              #Enumerate Service Control Manager DB
              >
              typeFilter = win32service.SE RVICE_WIN32
              stateFilter = win32service.SE RVICE_STATE_ALL
              >
              statuses = win32service.En umServicesStatu s(hscm, typeFilter, stateFilter)
              for (short_name, desc, status) in statuses:
              print short_name, desc, status

              Comment

              • Gabriel Genellina

                #8
                Re: Check a windows service

                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__=='__ma in__':
                EnumServices()

                Then execute it with: python your_file_name. py
                See
                Python offers a wide range of modules to simplify complex tasks. Learn what a module is, how to utilize them, and how to create your own modules in Python.

                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!


                Comment

                • Tim Golden

                  #9
                  Re: Check a windows service

                  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

                  Comment

                  • awel

                    #10
                    Re: Check a windows service

                    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

                    Comment

                    Working...