Page 1 of 1

Monitoring system service

Posted: 02 Apr 2010 01:57
by massimob
Hi,
I'm new in this forum and new for the dos :D.

I have the same problem:

I want to monitoring one my windows server 2000 system services if the state change from ACTIVATE to STOPPED.
For this, I have thought first to lunch a command: (now I use for example the windows service spooler)

1- sc query spooler ---> that return this:

Code: Select all

SERVICE_NAME: spooler
        TYPE               : 110  WIN32_OWN_PROCESS (interactive)
        STATE              : 4  RUNNING
                                (STOPPABLE,NOT_PAUSABLE,ACCEPTS_SHUTDOWN)
        WIN32_EXIT_CODE    : 0   (0x0)
        SERVICE_EXIT_CODE  : 0   (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0


That's the correct state of te service.
I put this state in a .txt file ---> sc query spooler > original_state.txt

I lunch this batch script evrey /n hour on the server for create a current_state.txt

Now I want to comparing the two file for analyse if the state of services change... for example if the current_state.txt contain this:

Code: Select all

SERVICE_NAME: spooler
        TYPE               : 110  WIN32_OWN_PROCESS (interactive)
        STATE              : 1  STOPPED
                                (STOPPABLE,NOT_PAUSABLE,ACCEPTS_SHUTDOWN)
        WIN32_EXIT_CODE    : 0   (0x0)
        SERVICE_EXIT_CODE  : 0   (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0


How can comparing a file and if are different lunch another .bat ???

For comparing, i can lunch: " fc original_state.txt currentent_state.txt /n " and this is the output:

Code: Select all

***** original_state.txt
    3:          TYPE               : 110  WIN32_OWN_PROCESS (interactive)
    4:          STATE              : 4  RUNNING
    5:                                  (STOPPABLE,NOT_PAUSABLE,ACCEPTS_SHUTDOWN)
***** current_state.TXT
    3:          TYPE               : 110  WIN32_OWN_PROCESS (interactive)
    4:          STATE              : 1  STOPPED
    5:                                  (STOPPABLE,NOT_PAUSABLE,ACCEPTS_SHUTDOWN)
*****


Any Idea??? how create an If or other control to analyse if two file are different or equal?
There is another metod???

Thank you in advance and sorry for my poor english.
waiting for your help.

Massimo

Re: Monitoring system service

Posted: 04 Apr 2010 23:00
by MarkL
Greetings!

Are you aware that fc sets the variable %ERRORLEVEL% to 0 if the files compare equal, which can be tested?

Re: Monitoring system service

Posted: 21 Feb 2012 10:03
by darioit
SC query liveupdate | FIND "STATE" | FIND "RUNNING"
if not %errorlevel%==0 CSCRIPT sent_email.vbs

Re: Monitoring system service

Posted: 21 Feb 2012 10:36
by Squashman
You could just use a for loop to set the current state of the process to a variable.

Code: Select all

for /f "tokens=3 delims=: " %%G in ('sc query spooler ^| find "STATE"') do set state=%%G