Help automating a script to restart a service


I have a server(2003 std) across town which has a memory leak in one of its services. The company who makes the application says the fix is to Disable AV scanning. I am not enthused about that.

I have found a work arround where I can monitor the Service from remotely and then manually restart it when the memory usage exceeds a threshold. Until now I have been using two tools from to do this. I use "pslist.exe  SERVICE" to poll the service every 5 seconds and once the PRIV memory field exceeds 500M I restart it from remote with the other tool "psservice.exe  restart   SERVICE" then it is fine untill I next have to bounce it again.

I can't keep manually doing it, because if I miss, someone (usually my boss) gets to drive across town to reboot it. If it isn't caught, all avalable memory is used and no one can log into the box remotely to kick start the service.

My End Goal and how I will award the points is a solution that automaticly restarts the service any time the service exceeds the memory threshold. The goal is a working solution. I would prefer something that uses Windows standard applications but that isnt a necessity. If you can figure a way to trigger my existing script that is fine too.

Thank you,
Rowan HawkinsSystem IntegratorAsked:
Who is Participating?
I wear a lot of hats...


Rowan HawkinsSystem IntegratorAuthor Commented:
I hesitate to just bounce the service every N hours because the speed at which the service uses memory is proportional to the number of users hitting the service and I want to limit disruptions for them, and have a solution that covers heavy usage times as well.

I can access the box any time to test solutions providing Im awake.

you could set performance monitor to poll availible memory and run net stop/start <service> to do restart to your faulty service:

Administrative Tools -> Performance -> Perf. Logs and Alerts -> add new Alert

Select Performance object "Memory" and counter Available Bytes/KBytes/MBytes (select which you like), Alert when the value is Under, Limit: <you choose>, Poll as you like (interval 5 mins, etc.)

Create batch file with commands:

net stop <yourservice>
net start <yourservice>

Set Action "Run this program" and set the command line to "C:\yourpath\yourbatchfile.cmd" (or what ever it is).

With this, when ever the availible memory is too low the service is restarted.

Hope this helps,

Rowan HawkinsSystem IntegratorAuthor Commented:
The problem with that is that memory can get low when SQL is running but then is released correctly. I dont want to have this service restarted a bunch of times when it isn't needed.  Every time the service is restarted all of the users have to log in again. Also during the time it is restarting it isnt doing what we need it to be doing.

Windows doesnt seem to keep track of individual apps in any meaningful way that I can find.

Exploring SharePoint 2016


Robberbaron (robr)Commented:
this script uses parts of your own solution (pslist) and bits from others as well.
It will need to run ON the remote server. So you are in for a trip across town :-< (or remote desktop !)

Run it as a test (using snmp as i did ?)
Then change the process name & run manually in a non-critical time

Set it as a Schduled task on the server in question, running as system account. You will have to logon as a Admin to be able to do this.  

Set the schedule at 5min.

Any shorter will chew up resources on the server. If the memory leak is so bad that this is too long then you have very serious troubles. Mind you, the script should still run as no login is required. Maybe set the limit lower to give more freeboard to the hard memory limit.

'Script to test a process memory use & restart
' RobG   ExpertsExchange   Jan 2006

Option Explicit
dim sProcessName      'name of process to test / restart
dim sResultLine       'line of text from PSInfo
Dim oShell, oFSO      ' WSH shell object, FSO object
dim oTextFile
dim sCommandLine      ' text to be run at command prompt
dim sDataFile             'name of temp data file
dim arrayDataItem
dim lMemUse           'memory in use
dim lMaxMem           'max mem before restart

'NB sProcessName is case sensitive.
sProcessName = "snmp"       'my test service
sDataFile = "psm_data.txt"
lMaxMem = 500000   'in kByte

'sysinternals PSInfo -m
'pslist -m explorer > psm_explorer.txt

sCommandLine = "c:\utils\pslist -m " & sProcessName & " > " & sDataFile

Set oShell = CreateObject("WScript.Shell")
oShell.Run "cmd /C " & sCommandLine,1
Wscript.sleep 1000
Set oShell = Nothing

'now readin the correct data line. it is the last one
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oTextFile = oFSO.OpenTextFile (sDataFile, 1)

Do Until oTextFile.AtEndOfStream
    sResultLine = oTextFile.Readline

set oFSO = Nothing

'test data
'Name                Pid      VM      WS    Priv      Priv Pk   Faults   NonP Page
'snmp               7544   35696    5404    2528    3264     1589      6   32
Wscript.Sleep 1000

'split the desird dataline into components
arrayDataItem= Split(sResultLine," ")

lMemUse = arrayDataItem(5)  ' the Priv VM usage

'check for mem use criteria
if lMemUse > lMaxMem then
      ' ReStartService.vbs
      ' Sample script to Stop or Start a Service
      ' Created by Guy Thomas December 2005 Version 2.4
      Dim objWMIService, objItem, objService
      Dim colListOfServices, strComputer, strService, intSleep
      strComputer = "."
      intSleep = 5000   'milliseconds
      'WScript.Echo " Click OK, then wait " & intSleep & " milliseconds"

      'On Error Resume Next
      ' NB strService is case sensitive.
      strService = " '" & sProcessName & "' "         '---modified RG ---
      Set objWMIService = GetObject("winmgmts:" _
            & "{impersonationLevel=impersonate}!\\" _
            & strComputer & "\root\cimv2")
      Set colListOfServices = objWMIService.ExecQuery _
            ("Select * from Win32_Service Where Name ="_
            & strService & " ")
      For Each objService in colListOfServices
            WSCript.Sleep intSleep
      'WScript.Echo "Your "& strService & " service has Started"
End if

set objService = Nothing
set objWMIService = Nothing


'----------- end of script -----------------

turn on the echo statements to test it's running ok.

Experts Exchange Solution brought to you by

Your issues matter to us.


Start your 7-day free trial
Rowan HawkinsSystem IntegratorAuthor Commented:
Thanks robberbaron,

           I'll give this a shot later this week and see how it works. Monday(tuesday) is always a day of putitng out the fires that came up over the weekend.
Rowan HawkinsSystem IntegratorAuthor Commented:
I just wanted to give an update, I didnt get to this last week and I am trying for this week.
Rowan HawkinsSystem IntegratorAuthor Commented:
robberbaron, just a quick question..  the script file wants to have a .WSH extension correct?
Robberbaron (robr)Commented:
No. I generally use .VBS   This is automatically associated with WSCRIPT.exe pr CSCRIPT.exe depending upon wether it is run from with Windows or commandline respectively.

I think WSH does extra un-necessary things.

1/ Try running it on your own PC, using the SNMP service as a test.
2/ try running it on the remote server, using snmp as test as this is not usually a critical service.
3/ what is the contents of psm_data.txt ?
Rowan HawkinsSystem IntegratorAuthor Commented:
that was my mistake, I'll Award RobberBarron the A the script worked perfectly.
Windows Server 2003

From novice to tech pro — start learning today.