Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2738
  • Last Modified:

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
0
alexr54
Asked:
alexr54
  • 10
  • 9
  • 3
  • +1
1 Solution
 
alexr54Author Commented:
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.
0
 
Tony MassaCommented:
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

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Tony MassaCommented:
My script would kill any iexplore.exe session over 500000 kb
0
 
alexr54Author Commented:
     Set colProcessList = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = 'iexplore.exe' AND workingSetSize > 500000")

Is where i can set the memory usage interval?

0
 
alexr54Author Commented:
Thanks, this looks good. I will try it out.
0
 
alexr54Author Commented:
It looks like it terminates right away, when the OK button is pushed.
0
 
alexr54Author Commented:
Also when first run, it terminates the process regardless of mem usage
0
 
Tony MassaCommented:
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
0
 
QlemoC++ DeveloperCommented:
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

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

wscript.sleep(180000) to force 3 minutes.
0
 
alexr54Author Commented:
tmassa99,

Do you know how i can remove the "OK" button from the popup? When pushed it triggers the closure to be immediate.
0
 
Tony MassaCommented:
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.
0
 
Tony MassaCommented:
Alex - If you add the WScript.Sleep(180000) line before objProcess.Terminiate, it won't kill it immediately.
0
 
Tony MassaCommented:
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

0
 
Tony MassaCommented:
Geez...sorry.  Fix the

WScript.Sleep(180000_

to

WScript.Sleep(180000)
0
 
alexr54Author Commented:
Thanks, works great!
0
 
alexr54Author Commented:
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
0
 
Tony MassaCommented:
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.
0
 
Tony MassaCommented:
This checks the user running the script, and if the process iexplore.exe is running as the same user ID, then it will get killed.
strComputer = "."
x = 2

On Error Resume Next

Set WSHNetwork = CreateObject("WScript.Network")
strCurrentUser = WSHNetwork.Username

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 While x = 2 ' This will run forever
	Set colProcessList = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = 'iexplore.exe' AND workingSetSize > 716800000")

	For Each objProcess in colProcessList
			colProperties = objProcess.GetOwner(strUser,strDomain)
			If strUser = strCurrentUser Then
				objShell.Popup strPopUpText, strPopupTimeout, strPopUpTitle
				WScript.Sleep(180000)
				objProcess.Terminate
			End If
	Next
	WScript.Sleep(300000) 'Five minute wait to try again
Loop

Open in new window

0
 
QlemoC++ DeveloperCommented:
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

0
 
QlemoC++ DeveloperCommented:
Any results yet?
0
 
alexr54Author Commented:
Yes tmassa99 solution did the trick.

Thanks so much
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 10
  • 9
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now