Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Setting up notification of an NT service state

Posted on 2011-10-04
5
Medium Priority
?
299 Views
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.
0
Comment
Question by:garychu
  • 3
5 Comments
 
LVL 43

Accepted Solution

by:
Steve Knight earned 1000 total points
ID: 36914908
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...

e.g.

@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



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

Assisted Solution

by:BillDL
BillDL earned 1000 total points
ID: 36917999
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 ;-).
 NET-HELPMSG-Descriptions.xls

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"
or:
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.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36919607
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?

steve
0
 

Author Closing Comment

by:garychu
ID: 36921902
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.
Cheers
0
 
LVL 43

Expert Comment

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

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Integration Management Part 2
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses

580 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