Link to home
Create AccountLog in
Avatar of FusionApps
FusionApps

asked on

Determine Process Memory Usage - Kill Service If Threshold Not Reached

I'm trying to finagle some robustness into a flakey game server that I am running.  I am using FireDaemon to run the game as a service when the server boots up.  Sometimes, the game doesn't fully launch for an unknown reason.  The one thing that I have noticed is that when the launch fails, the game is using less than 50MB of RAM.  On a successful launch, the game uses about 130MB of RAM.  So I'm trying to handle this condition with a script that will monitor the memory usage of the EXE, and take action accordingly.

I have the attached Code Snippet that outputs the memory usage of a windows process "COJ.exe" in Bytes.  I want to be able to detect when this process is below a threshold of 50MB or 52429000 Bytes.  There is a Pre-Service Program section in FireDaemon that allows me to plug in a program to execute before the service is started.  I want this script to run for a maximum of 5 minutes checking once every minute, and once COJ.exe is using more than 50MB of RAM, this script can exit.  But, if the condition is not met within 5 minutes, I want to kill the service.

Any help is much appreciated.
'----- BEGIN SCRIPT
On Error Resume Next
strComputer = "."
strTaskName = "COJ.exe"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Process Where Caption = '" & strTaskName &"'" ,,48)
For Each objItem in colItems
    Wscript.Echo "WorkingSetSize: " & objItem.WorkingSetSize
Next
'----- END SCRIPT

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of FusionApps
FusionApps

ASKER

Thanks, the script looks good, I will give it a shot probably tonight.
No problem.  Let me know how it goes....do you know the service name of your service, as you would type it at the command prompt with
net stop "service name"

I think the display name when viewed in Computer Management --> Services would do.

Regards,

Rob.
Hi, did this script end up working for you?

Regards,

Rob.
Yes it did
Great! Thanks for the grade.

Regards,

Rob.
Hi Rob.

I need to use this script as well. I have modified it for my needs. Can you please let me know how to execute it on my server?

I am running the following:

- WIndows Server 2003 (latest patch)
- JRUN 4

I have modified the script to bounce our JRUN process when the memory it uses reaches 900Mb. The JVM Heap has 1 Gb allocated, so this represents ~ 90% RAM use by the process.

I am not familiar with creating VB Scripts on a Windows server. I am hoping to get thi issue resolved asap. THanks for any help you can offer.

Regards,

Ben Marshall
webmaster@shopdi.com


On Error Resume Next
' PARAMETERS
intMinutes = 3
strComputer = "."
strTaskName = "jrun.exe"
strServiceName = "ColdFusion MX 7 Application Server"
' END PARAMETERS
 
boolRunningProperly = False
intSize = 0
 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
While boolRunningProperly = False And intMinutes > 0
 
        ' Wait one minute first to give it time to start
        ' WScript.Sleep 60000 **commented out BGM - no delay needed**
        intMinutes = intMinutes - 1
 
        Set colItems = objWMIService.ExecQuery("Select * from Win32_Process Where Caption = '" & strTaskName &"'" ,,48)
        intSize = 0
        For Each objItem in colItems
            intSize = objItem.WorkingSetSize
        Next
        boolRunningProperly = False
		
        If intSize = 0 Then
                'WScript.Echo strTaskName & " is not running."
        Else
                intSizeMB = intSize / 1024000
                'WScript.Echo intSize & VbCrLf & intSizeMB & " MB"
                If intSizeMB < 900 Then boolRunningProperly = True
        End If
        
        If boolRunningProperly = True Then
                WScript.Echo "The program is running properly."
                WScript.Quit
        Else
                WScript.Echo "The program is not running properly. Checking again..."
        End If
Wend
 
If boolRunningProperly = False Then
        Set objShell = CreateObject("WScript.Shell")
        objShell.Run "cmd /c net stop """ & strServiceName & """", 0, True
        objShell.Run "cmd /c net start """ & strServiceName & """", 0, True
        WScript.Echo "The service has been restarted."
End If

Open in new window

Hi Rob.

I figured it out myself. I just made the script have a .VBS extension and it ran perfectly. Thanks for the code in your original question. I would add a grade if I could, but this topic was already closed. Much appreciated nonetheless.

Regards,
Ben
No worries Ben.  Don't worry about the grade, EE's solutions are here for others to use.

I'm glad you found my code useful.  Thanks.

Regards,

Rob.