Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Script to check if Service is running every 5 minutes, if not running restart Service

Posted on 2007-11-20
6
Medium Priority
?
4,259 Views
Last Modified: 2013-11-30
Hello Experts,

I am using VNC (running as a Service) on a Server.  
Sometimes it crashes.
I am looking for a way of ensuring the VNC Service (winvnc.exe) gets restarted if it crashes.

What I have in mind is a Script that will:

* check if winvnc.exe is running every 5 minutes,
* if it is not running restart it

Regards,
Leigh
0
Comment
Question by:LeighWardle
  • 3
  • 2
6 Comments
 
LVL 2

Accepted Solution

by:
gmilhon earned 2000 total points
ID: 20323936
You can configure the service to restart upon failure. In the Services control panel, right click the VNC Server service and select Properties. On the "Recovery" tab, you can set the action for the 1st Failure to Restart, you can do the same for the subsequent failures.
0
 
LVL 1

Author Comment

by:LeighWardle
ID: 20324004
Hi gmilhon,

Too easy!
Your answer proves the value of using experts-exchange.com.
I had no idea that those options were available in Services/Recovery.

Thanks again,

Leigh
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 20324050
Hello again Leigh, please try this script, placing a list of computer names to check the service on, into a file called computers.txt

'============
If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
    strPath = Wscript.ScriptFullName
    strCommand = "%comspec% /k cscript  """ & strPath & """"
    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run(strCommand), 1, True
    Wscript.Quit
End If

' Note: strService is case sensitive.
strService = "VNC Server"
strInputFile = "computers.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1

' Create an endless loop to run every 5 minutes
While 1 <> 2
      Set objInputFile = objFSO.OpenTextFile(strInputFile, intForReading, False)
      While Not objInputFile.AtEndOfStream
            strComputer = objInputFile.ReadLine
            If Ping(strComputer) = True Then
                  Set objWMIService = GetObject("winmgmts:" _
                        & "{impersonationLevel=impersonate}!\\" _
                        & strComputer & "\root\cimv2")
                  Set colListOfServices = objWMIService.ExecQuery _
                        ("Select * from Win32_Service Where DisplayName='"& strService & "'")
                  On Error Resume Next
                  For Each objService In colListOfServices
                        If Err.Number = 0 Then
                              On Error GoTo 0
                              WScript.Echo "State of " & strService & " service on " & strComputer & ": " & objService.State
                              WScript.Echo ""
                              If objService.State <> "Running" Then
                                    WScript.Echo "Starting " & strService & " service on " & strComputer
                                    objService.StartService()
                                    WScript.Sleep 5000
                              End If
                        Else
                              Err.Clear
                              On Error GoTo 0
                              WScript.Echo strService & " service was not found on " & strComputer
                        End If
                  Next
            Else
                  WScript.Echo ""
                  WScript.Echo strComputer & " could not be pinged."
            End If
      Wend
      objInputFile.Close
      Set objInputFile = Nothing
      WScript.Echo "Waiting 5 minutes. Press CTRL + C to exit script."
      WScript.Sleep 300000
Wend

WScript.Echo ""
WScript.Echo "Script finished."

Function Ping(strComputer)
      Dim objShell, boolCode
      Set objShell = CreateObject("WScript.Shell")
      boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
      If boolCode = 0 Then
            Ping = True
      Else
            Ping = False
      End If
End Function
'============

Regards,

Rob.
0
Technology Partners: 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!

 
LVL 65

Expert Comment

by:RobSampson
ID: 20324059
Oh, ha ha, that approach is much easier!  Nice one!

Regards,

Rob.
0
 
LVL 1

Author Comment

by:LeighWardle
ID: 20324090
Hi Rob,

Thanks for your efforts.

But I am glad I got to hear about the easy method first!

All the best,

Leigh
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 20324120
LOL! Yeah, absolutely!  Despite the fact it takes a small amount of manual configuration, it is definately easier, and saves time in the long run!

Regards,

Rob.
0

Featured Post

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!

Question has a verified solution.

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

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
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…
How to install and configure Citrix XenApp 6.5 - Part 1. In this video tutorial we have explained step by step installation of Citrix XenApp 6.5 Server on Windows Server 2008 R2 is explained in this video. We have explained the difference between…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

916 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