Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Restart service if it is not running.

Posted on 2012-04-03
6
Medium Priority
?
401 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
[X]
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
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 2000 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

722 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