Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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,258 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
[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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
Remote Desktop Shadowing often has a lot of benefits. When helping end users determine problems, it is much easier to see what is going on, what is being slecected and what is being clicked on. While the industry has many products to help with this,…
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

722 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