Setting up notification of an NT service state

Posted on 2011-10-04
Last Modified: 2012-05-12
I use a command;
net start "<service-name>" to start and
net stop "service-name>" to stop the service.
I do this in a batch file, scheduled to run unattended.
Are there command(s) I can include in the batch file to check if the service starts/stop successfully? Such that if a return code is detected, I could then use a utility, say, Postie to email out notifications to individuals.
Given the objective, would this be so old world, and that there are more contemporary ways of doing so?
Thanks, Experts.
Question by:garychu
    LVL 43

    Accepted Solution

    there are other ways, but if you are happy with that why not.  i cant check what thwe errorlevels are at the moment but would assume net start / stop do have errorlevels, if not we could parse their output...


    @echo off
    set log=c:\dir\logfile.txt
    Net stop >%log% 2>&1
    set stoperror=%errorlevel%
    if %stoperror% GEQ 1 call error STOP %stoperror%

    net start >>%log% 2>>&1
    set starterror=%errorlevel%
    if %starterror% GEQ 1 call error START %starterror%

    net start |find /i "Service Name" || call error "NOT STARTED" 0

    exit /b

    echo %date% %time% There was an error %~2 during %~1 >> %log%
    echo Send message here... Postie, blar, vbscript etc. %log% contains error info.
    LVL 38

    Assisted Solution

    This is just informative.  dragon_it is very conversant with the "best methods" for what you want to achieve, and may return with more info.

    From my previous notes these are the NET command's exit codes, but I cannot remember where I got them from originally:

    0 = Success
    1 = Not Supported
    2 = Access Denied
    3 = Dependent Services Running
    4 = Invalid Service Control
    5 = Service Cannot Accept Control
    6 = Service Not Active
    7 = Service Request Timeout
    8 = Unknown Failure
    9 = Path Not Found
    10 = Service Already Running
    11 = Service Database Locked
    12 = Service Dependency Deleted
    13 = Service Dependency Failure
    14 = Service Disabled
    15 = Service Logon Failure
    16 = Service Marked For Deletion
    17 = Service No Thread
    18 = Status Circular Dependency
    19 = Status Duplicate Name
    20 = Status Invalid Name
    21 = Status Invalid Parameter
    22 = Status Invalid Service Account
    23 = Status Service Exists
    24 = Service Already Paused

    The descriptions can be pretty ambiguous though.  For example you will usually get an exit code of 2 if you type an invalid service name!

    There is also the informative line you will see when something goes wrong, like:
    "The requested service has already been started. More help is available by typing NET HELPMSG 2182"
    The HELPMSG number does not relate in any way to the error codes from what I have determined.  They go up to 4 digits, but not all are used (attached Excel File with ones used in WinXP if you really, really want to know - not all output lines checked though ;-).

    You can, however, use the NET HELPMSG numbers something like this as an alternative test (on the FIND command rather than the NET command) without seeing the program output on screen:

    NET START SomeService 2>&1 | FIND "2182"
    IF ERRORLEVEL 0 ECHO SomeService already started & GOTO :ServiceStarted

    The NET HELPMSG number may also appear in an error message, for example if you try to run a NET STOP command on an unknown (not installed) Service:
    "System error 1060 has occurred.
    The specified service does not exist as an installed service."

    Remember that when you have multiple Exit Codes where more than one could apply and you want to specifically test for more than one, start with the highest one when testing:

    if errorlevel 24 echo Some Message & goto :Code24
    if errorlevel 23 echo Some Other Message & goto :Code23
    etc, etc
    if errorlevel 0 echo Success & goto :EOF

    The default    IF ERRORLEVEL 1    means 1 and above.
    You can instead use:
    if %errorlevel"=="1"
    if %errorlevel% equ 1
    to test for the exact exit code.

    Other Compare-Ops are:
    neq - Not Equal To
    gtr - Greater Than
    lss - Less Than
    geq - Greater Than Or Equal To
    leq - Less Than Or Equal To
    The /i (treat as case insensitive) switch with the IF command is not applicable to numbers).

    As I say, just informative.
    LVL 43

    Expert Comment

    by:Steve Knight
    thanks bill.  out and about still today and tomorrow too.... Paid work so cant knock it ... So not much time to be here.

    i hadnt seen that list of error codes before for net.exe so is useful here too.

    Should be able to use sc.exe too if available i suppose.

    Garychu -has that given you enough between us (pls split points of course) or need some more clarification?


    Author Closing Comment

    Thanks, Experts.
    I am grateful for the trouble taken and the benefits of knowledge and experience shared.
    I think I now have enough to try out a few things before coming to a final solution.
    LVL 43

    Expert Comment

    by:Steve Knight
    No problem.... Just ask another q if you need more and we'll see it.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
    Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now