No announcement yet.

detect if service is running

  • Filter
  • Time
  • Show
Clear All
new posts

  • detect if service is running

    is there a way to detect whether or not a service is running?

    I have a Windows server giving that for some reason will just turn off its IIS.
    It is very sporadic. I've been going through the event logs, looking for patches, viruses, etc.

    In the meantime I'd like to have something running that would automatically restart it if it stops. We already run Nagios, which tells me if its down but I want to be able to automatically restart it. The server is a couple hundred miles away and I have to remote in to restart IIS, which isn't always practical.


  • #2
    There may be a better way, but....

    If you know the name of the EXE file for IIS, you can enumerate all the processes running, get the name of the EXE files associated with those processes, and see if that EXE is one of those.

    There may be better examples, but you can get all the code you need to do that here:
    Show Loaded Modules Source and Executable package for PB and VB
    Visual Basic include files/demo code by Balthasar Indermuehle. (PB code by YT.)

    This method of course fails if IIS is in a "waiting to be terminated" state (e.g., "IIS has encountered a program error and must be terminated...."), but there may be a way to query the process using to detect that.

    If it's terminated, execute the Exe file with proper command line to restart.

    Come to think of it...

    If you get a process handle via this enumeration, no reason you can't wait on that handle to reach a signalled state... meaning the process has ended. That would give you an instant "I know it's over" (that sounds like the title of a love song, doesn't it?) notification and you could restart immediately.

    Last edited by Michael Mattias; 5 Mar 2008, 11:30 AM.
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]


    • #3
      See those api call...

      OpenSCManager, OpenService, ControlService, GetServiceDisplayName,
      GetServiceKeyName, EnumServicesStatus, EnumDependentServices,
      LockServiceDatabase, UnlockServiceDataBase, ChangeServiceConfig,
      QueryServiceConfig, QueryServiceStatus,
      QueryServiceLockStatus and CloseServiceHandle.

      Also have a look at Service Start/Stop


      • #4
        >See th[e]s api call[s]...

        It kind of shows I don't do services, huh?
        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]


        • #5
          Here is a code sample to see if services are running:

          PowerBASIC and related source code. Please do not post questions or discussions, just source code.
          Adam Drake