Solved

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

Posted on 2007-11-20
6
4,254 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 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

Explore all the enhancements 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help Creating PowerShell Script 5 66
Index Match Formula VBA 6 52
need assistance with a VBscript 3 39
using vb script 5 52
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

733 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