Solved

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

Posted on 2007-11-20
6
4,250 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
Remote Desktop Connections allow you to control remote host machines via the magic of the Internet and RDP (Remote Desktop Protocol). For the purposes of this article we will assume you are connecting from your home PC or laptop to a remote offic…
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now