Solved

Restart service if it is not running.

Posted on 2012-04-03
6
390 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Suggested Solutions

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

738 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