Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Restart service if it is not running.

Posted on 2012-04-03
6
387 Views
Last Modified: 2012-04-08
Is there a batch or vbscript file that can check to see if a service is running.  If a service is running then the script will do nothing.  If the service isn't running then it would start the service.

Also, the same for a process.  If the process isn't running then it would start the process.

Thank you.
0
Comment
Question by:cbones
6 Comments
 
LVL 20

Expert Comment

by:n2fc
ID: 37804229
See: http://technet.microsoft.com/en-us/library/cc773035(v=ws.10).aspx

The command: NET START {service}  will start a service; if it is already running, it will just tell you so!
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 37804235
Hi, you can use this for a service:
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

strComputer = "."
strServiceName = "spooler"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service Where Name='" & strServiceName & "'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objItem In colItems
	If objItem.State <> "Running" Then objItm.StartService()
Next

Open in new window


and this for a process:
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

strComputer = "."
strProcessPath = "c:\temp\myprogram.exe"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service Where Name='" & Mid(strProcessPath, InStrRev(strProcessPath, "\") + 1) & "'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
blnFound = False
For Each objItem In colItems
	blnFound = True
Next
If blnFound = False Then
	Set objShell = CreateObject("WScript.Shell")
	objShell.Run strProcessPath, 1, False
End If

Open in new window


Regards,

Rob.
0
 
LVL 2

Expert Comment

by:FostWare
ID: 37804236
If you're running Windows 2008 or 7, you can set an event log task to restart the service.

We use it because it doesn't require us to have an extra service constantly running the vbscript.

Event log tasks can be set by selecting a specific error log entry for your service, clicking "Attach a task to this log..." and setting the task to cmd /c "sc start MyService"
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

Author Comment

by:cbones
ID: 37806115
Rob,

I receive an error when running the first vbscript.  I attached the file.  The process file just runs with no action.

Thank you.
Windows-Script-Host-Error.JPG
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 37808888
Sorry, missed the "e" in objItem.  Here is the correct "service" code.

Rob.

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

strComputer = "."
strServiceName = "spooler"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service Where Name='" & strServiceName & "'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objItem In colItems
	If objItem.State <> "Running" Then objItem.StartService()
Next

Open in new window

0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 37809547
Ooop, just saw a mistake in the process code as well.  I accidentally left in Win32_Service instead of Win32_Process

Regards,

Rob

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

strComputer = "."
strProcessPath = "c:\temp\myprogram.exe"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Process Where Name='" & Mid(strProcessPath, InStrRev(strProcessPath, "\") + 1) & "'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
blnFound = False
For Each objItem In colItems
	blnFound = True
Next
If blnFound = False Then
	Set objShell = CreateObject("WScript.Shell")
	objShell.Run strProcessPath, 1, False
End If

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

790 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