Link to home
Start Free TrialLog in
Avatar of alexr54
alexr54Flag for United States of America

asked on

Script to Kill process using over 700MB memory

Can i please have help with a script which can run in the background constantly that will kill a process with a specific name like iexplore.exe which is using more then 700MB memory in a specified amount of time?

Also at the same time before killing the process send a message popup warning to the PID user sayinfg something like "Your app will close due to high memory usage in 3 minutes, please save all your work"

Just to be more clear on this:

I am running a home made app over citrix. This app seems to have some memory issues, and when the memory utilization is high, it slows down the server considerably. The process runs on the admin user account, but of course has a seperate PID based on the user who logged in.

If i can warn the users, and then close the high memory usage app automatically, this would be a nice temporary fix until the apps memory issue is fixed.

Its run on windows 2003 r2
Avatar of plusone3055
plusone3055
Flag of United States of America image

Avatar of alexr54

ASKER

tasklill does not run in the background, it must be executed at the time of a process kill no?

Also there is no message and timer option as far as i can see.
Avatar of Tony Massa
This may help, but it doesn't identify the user.  Perhaps if it's run under the user context (at logon), they may only be able to see their own processes.  You may also have to tweak the timing of the POPUP box and Script sleeping
strComputer = "."

On Error Resume Next
strPopUpText = "To improve server performance we need to close your session" & _
	vbCRLF & vbCRLF & "Please save your work NOW.  Your session will automatically terminate in 3 minutes"
strPopUpTimeout = "180" 'Timeout in seconds
strPopUpTitle = "Application Restart Required"

	Set objShell = CreateObject("WScript.Shell")
	Set objFSO = CreateObject("Scripting.FileSystemObject")

	Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Do Until x = 2
	Set colProcessList = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = 'iexplore.exe' AND workingSetSize > 500000")

	For Each objProcess in colProcessList
			objShell.Popup strPopUpText, strPopupTimeout, strPopUpTitle
			objProcess.Terminate
	Next
	WScript.Sleep(300000) 'Five minute wait to try again
Loop

Open in new window

My script would kill any iexplore.exe session over 500000 kb
Avatar of alexr54

ASKER

     Set colProcessList = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = 'iexplore.exe' AND workingSetSize > 500000")

Is where i can set the memory usage interval?

Avatar of alexr54

ASKER

Thanks, this looks good. I will try it out.
Avatar of alexr54

ASKER

It looks like it terminates right away, when the OK button is pushed.
Avatar of alexr54

ASKER

Also when first run, it terminates the process regardless of mem usage
Line 17 should read:

("Select * from Win32_Process Where Name = 'iexplore.exe' AND workingSetSize > 512000000")

when divided by 1024, that would get 500,000 kb
This would be the batch file running on the console (admin) session endlessly, checking for iexplore and size of 700MB.
@echo off
setlocal EnableDelayedExpansion

:endless
for /F "tokens=1,2,4,5 delims=," %%A in ('tasklist /fi "memusage gt 700000" /fi "imagename eq iexplore.exe" /nh /fo csv ^| find /V "INFORMATION:"') do (
  echo,Process %%A with PID %%B in session %%C uses %%D memory
  msg %%C /time:300 /w Your app will close due to high memory usage in 3 minutes, please save all your work
  taskkill /PID %%B /f
)
REM wait 300 sec.
choice /T 300 /C CA /D C /M "Continue or Abort"
goto endless

Open in new window

and before objProcess.Terminate, add a new line with:

wscript.sleep(180000) to force 3 minutes.
Avatar of alexr54

ASKER

tmassa99,

Do you know how i can remove the "OK" button from the popup? When pushed it triggers the closure to be immediate.
Qlemo -
Can that run under the system account, or would an admin have to log on to run it?  If a user acknowledges the message, will the taskkill process be initiated immediately?  My popup box was similar, in that, when acknowledged, the process would be killed immediately without the user having the option of waiting.  You may also need a wait command there as well.

Alex -
The workingsetsize is the amount of memory in bytes.  716800000 = 700,000 KB as seen in task manager.
Alex - If you add the WScript.Sleep(180000) line before objProcess.Terminiate, it won't kill it immediately.
Here's the updated script with the forced 3 minute wait.  Just change the workingsetsize number to fit your needs.
strComputer = "."

On Error Resume Next
strPopUpText = "To improve server performance we need to close your session" & _
	vbCRLF & vbCRLF & "Please save your work NOW.  Your session will automatically terminate in 3 minutes"
strPopUpTimeout = "180" 'Timeout in seconds
strPopUpTitle = "Application Restart Required"

	Set objShell = CreateObject("WScript.Shell")
	Set objFSO = CreateObject("Scripting.FileSystemObject")

	Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Do Until x = 2
	Set colProcessList = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = 'iexplore.exe' AND workingSetSize > 716800000")

	For Each objProcess in colProcessList
			objShell.Popup strPopUpText, strPopupTimeout, strPopUpTitle
			WScript.Sleep(180000_
			objProcess.Terminate
	Next
	WScript.Sleep(300000) 'Five minute wait to try again
Loop

Open in new window

Geez...sorry.  Fix the

WScript.Sleep(180000_

to

WScript.Sleep(180000)
Avatar of alexr54

ASKER

Thanks, works great!
Avatar of alexr54

ASKER

oops still a problem.. It dosnt just close my IE it closes every users IE, even if the script is not run on that user account. It closes with no warning message
In the mean time, try the other solution posted here.

I don't think that WMI exposes the user running the process, but I'll check.
ASKER CERTIFIED SOLUTION
Avatar of Tony Massa
Tony Massa
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here the batch version which will always wait 180 sec. before killing. The wait & kill is running in a separate task, so if more than one process is to kill, they are processed as quick as possible.
@echo off
setlocal EnableDelayedExpansion

:endless
for /F "tokens=1,2,4,5 delims=," %%A in ('tasklist /fi "memusage gt 700000" /fi "imagename eq iexplore.exe" /nh /fo csv ^| find /V "INFORMATION:"') do (
  echo,Process %%A with PID %%B in session %%C uses %%D memory
  msg %%C Your app will close due to high memory usage in 3 minutes, please save all your work
  start /min "Killing %%A (%%B)" choice /T 180 /C N /D N /M "Wait ..." & taskkill /PID %%B /f
)
REM wait 300 sec.
choice /T 300 /C CA /D C /M "Continue or Abort"
goto endless

Open in new window

Any results yet?
Avatar of alexr54

ASKER

Yes tmassa99 solution did the trick.

Thanks so much