Solved

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

Posted on 2007-11-20
6
4,252 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 500 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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBS Script not working correctly. 1 41
VbScript to countdown to New Year's Day 6 55
Remote access software other than RDP 10 35
change the windows script file to BAT 10 41
Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
Let’s list some of the technologies that enable smooth teleworking. 
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
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…

773 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