views:

6240

answers:

8

I need to know the status of a service at the end of my batch script which restarts services using "net stop thingie" and "net start thingie".

In my most favorite ideal world, I would like to e-mail the state to myself, to read on cold winter nights, to reassure myself with the warmth and comfort of a server that I know is running right.

Just for you to know, I'm using a Windows server 2003 platform, and a batch file seemed the best choice. I don't mind using something else, and would be very open to suggestions, but just for the sake of knowledge (as a zombie craves brains, I thought, why not inflate my own), is there a command that allows me to check on the status of a service, in command line?

Should I just redirect the output of the command to a file?

Where the hell are my pants? (Gosh, I really do hope the humor inserted in this will not insult anyone. It's wednesday morning, and humor I do need too :P)

[Edit:] The solution I used is available for download from http://thezombie.net/service_restart.zip

It is used as a task set to be executed during the night, and checking my e-mail in the morning, I see whether or not the service has correctly restarted.

Thanks to all!

+3  A: 

Using pstools - in particular psservice and "query" - for example:

psservice query "serviceName"
Marc Gravell
+2  A: 

Using Windows Script:

Set ComputerObj = GetObject("WinNT://MYCOMPUTER")    
ComputerObj.Filter = Array("Service")    
For Each Service in ComputerObj    
    WScript.Echo "Service display name = " & Service.DisplayName    
    WScript.Echo "Service account name = " & Service.ServiceAccountName    
    WScript.Echo "Service executable   = " & Service.Path    
    WScript.Echo "Current status       = " & Service.Status    
Next

You can easily filter the above for the specific service you want.

MaSuGaNa
Selected because no installation required. Thank you guys!
MrZombie
A: 

according to this http://www.computerhope.com/nethlp.htm it should be NET START /LIST but i can't get it to work on by XP box. I'm sure there's some WMI that will give you the list.

Nick Kavadias
+4  A: 

Have you tried sc.exe?

C:\> for /f "tokens=2*" %a in ('sc query audiosrv ^| findstr STATE') do echo %b
4  RUNNING

C:\> for /f "tokens=2*" %a in ('sc query sharedaccess ^| findstr STATE') do echo %b
1  STOPPED

Note that inside a batch file you'd double each percent sign.

Romulo A. Ceccon
+1  A: 

You can call net start "service name" on your service. If it's not started, it'll start it and return errorlevel=0, if it's already started it'll return errorlevel=2.

JRL
Not sure why I got a downvote on this but since he's using a bat file to start the service with the net start command, and it's built into the command to return an error code, the easiest is to simply check the error code. But if it's mandatory to create a separate vbs for it instead of checking the return, I don't care.
JRL
+1  A: 

Well I'm not sure about whether you can email the results of that from a batch file. If I may make an alternate suggestion that would solve your problem vbscript. I am far from great with vbscript but you can use it to query the services running on the local machine. The script below will email you the status of all of the services running on the machine the script gets run on. You'll obviously want to replace the smtp server and the email address. If you're part of a domain and you run this script as a privileged user (they have to be an administrator on the remote machine) you can query remote machines as well by replacing localhost with the fqdn.

Dim objComputer, objMessage
Dim strEmail

' If there is an error getting the status of a service it will attempt to move on to the next one
On Error Resume Next

' Email Setup
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Service Status Report"
objMessage.From = "[email protected]"
objMessage.To = "[email protected]"
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.example.net"

'Server port (typically 25)
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25

Set objComputer = GetObject("WinNT://localhost")
objComputer.Filter = Array("Service")

For Each aService In objComputer
strEmail = strEmail &chr(10) & aService.Name & "=" & aService.Status
Next

objMessage.TextBody = strEmail
objMessage.Configuration.Fields.Update
objMessage.Send

Hope this helps you! Enjoy!

Edit: Ahh one more thing a service status of 4 means the service is running, a service status of 1 means it's not. I'm not sure what 2 or 3 means but I'm willing to bet they are stopping/starting.

TrueDuality
Thanks alot for that script! Will be using it appended in my batch file, in the simple yet efficient form of a shell call to a vb script, which is quite easily handled by the server :P
MrZombie
That combined with the other script that I have selected as a good answer, I got my stop/restart/mail result cycle right about done.
MrZombie
+2  A: 

If PowerShell is available to you...

Get-Service -DisplayName *Network* | ForEach-Object{Write-Host $_.Status : $_.Name}

Will give you...

Stopped : napagent
Stopped : NetDDE
Stopped : NetDDEdsdm
Running : Netman
Running : Nla
Stopped : WMPNetworkSvc
Stopped : xmlprov

You can replace the **Network** with a specific service name if you just need to check one service.

aphoria
A: 

look also hier:

NET START | FIND "Service name" > nul IF errorlevel 1 ECHO The service is not running

just copied from: http://ss64.com/nt/sc.html

Ros

related questions