Solved

Restart service if it is not running.

Posted on 2012-04-03
6
388 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
Independent Software Vendors: 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

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!

Question has a verified solution.

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

Suggested Solutions

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
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…

763 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