Monitoring system service

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
massimob
Posts: 1
Joined: 02 Apr 2010 01:26

Monitoring system service

#1 Post by massimob » 02 Apr 2010 01:57

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

MarkL
Posts: 2
Joined: 04 Apr 2010 22:53

Re: Monitoring system service

#2 Post by MarkL » 04 Apr 2010 23:00

Greetings!

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

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Monitoring system service

#3 Post by darioit » 21 Feb 2012 10:03

SC query liveupdate | FIND "STATE" | FIND "RUNNING"
if not %errorlevel%==0 CSCRIPT sent_email.vbs

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Monitoring system service

#4 Post by Squashman » 21 Feb 2012 10:36

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

Post Reply