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: 1301
  • Last Modified:

Tasklisk and taskkill bats

Hello,

I have a software that multiple users on a terminal server use and I am trying to create a bat file to kill a specific process when becomes non responsive. Here is the scenario. At any given time i could have 40+ users logged in all using a process [wrun32.exe] but it sometimes it will hang at 25% processor utilization. Needless to say if four users have processes the server is locked up. I created the following script to find the hung process and send its process ID as a variable that i can use taskkill to close.

::Kill WRUN ON SERVER01

:SERVER01
for /f "usebackq tokens=2" %%a in (`tasklist /s SERVER01 /FO list /FI "IMAGENAME eq wrun32.exe" /FI "STATUS eq NOT RESPONDING" ^| find /i "PID:"`) do set PID=%%a
IF %PID%=NUL GOTO END Else taskkill /F /S SERVER01 /pid %PID%

:END
cls

I need to find a way to get this to only run on a process that is hung with the CPU utilization at 25% and run every 5 minutes. Any suggestions?
0
reddredson
Asked:
reddredson
  • 7
  • 5
1 Solution
 
Michael PfisterCommented:
I'd rather fix the problem instead of creating a workaround.

Is this Acuserver?

Try http://www.symantec.com/connect/forums/acuserver-problem

Basically they are excluding every directory that has to do with the software from the antivirus program.

If this is not the solution, a VBScript could do that:

http://gallery.technet.microsoft.com/scriptcenter/49489911-f686-4a74-98f8-ed121fd7d544
0
 
reddredsonAuthor Commented:
I wish is was as simple as an antivirus problem but it is caused from a program our company uses called Persona written from AcuCOBOL-GT. From what i gather [ wrun32.exe *32 ] it functions to emulate the job of a multi-processor server so multiple users can share the use of it but I can only find instances where the issue occurs on quad-core processor machines.
0
 
Michael PfisterCommented:
Can the VBScript help you to find processes consuming the CPU?
0
Industry Leaders: 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!

 
reddredsonAuthor Commented:
The script does show the running processes but i already know the process name. Its "wrun32.exe *32".
0
 
Michael PfisterCommented:
The script was just a sample, I've modified it to filter just for wrun32. Check what it will show for a hanging wrun32.exe

strComputer = "." 
Set objWMIService = GetObject("winmgmts:" _ 
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
set objRefresher = CreateObject("WbemScripting.SWbemRefresher") 
Set colItems = objRefresher.AddEnum _ 
 (objWMIService, "Win32_PerfFormattedData_PerfProc_Process").objectSet 
objRefresher.Refresh 
 
  Wscript.Echo Now 
  For Each objItem in colItems 
    if objItem.Name = "wrun32" then
    objRefresher.Refresh 
    Wscript.Echo vbCrLf & "Name: " & objItem.Name 
    Wscript.Echo "  Creating Process ID: " & objItem.CreatingProcessID 
    Wscript.Echo "  Percent Processor Time: " & _ 
      objItem.PercentProcessorTime 
    End if
  Next 

Open in new window

0
 
reddredsonAuthor Commented:
It does show the process and current cpu load but it only will show one. I could have the same process running 40 times with the same name but only one of them will be hung at 25%.  Once i find that process that is hung at 25% i need to kill it.  The wrun32.exe will be running but never get over 5% but on rare occasions it will spike to 25% and hang.
0
 
Michael PfisterCommented:
Try this please. I wasn't aware that it the function will add #1, #2 and so on for each instance ...

strComputer = "." 
Set objWMIService = GetObject("winmgmts:" _ 
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
set objRefresher = CreateObject("WbemScripting.SWbemRefresher") 
Set colItems = objRefresher.AddEnum _ 
 (objWMIService, "Win32_PerfFormattedData_PerfProc_Process").objectSet 
objRefresher.Refresh 
 
  Wscript.Echo Now 
  For Each objItem in colItems 
   if Instr(objItem.name,"wrun32") > 0 then
    objRefresher.Refresh 
    Wscript.Echo vbCrLf & "Name: " & objItem.Name 
    Wscript.Echo "  Creating Process ID: " & objItem.CreatingProcessID 
    Wscript.Echo "  Percent Processor Time: " & _ 
      objItem.PercentProcessorTime 
    End if
  Next 

Open in new window

0
 
reddredsonAuthor Commented:
Few questions:
1. The actual PID for the process is completely different. Where are these Process ID's coming from?
2. Not sure how this is supposed to help when i already know the process and the current PID. Is it a way to echo the process current process utilization? Maybe export it as a variable? Is this VB script only identifying the processes?
0
 
Michael PfisterCommented:
1. Sorry, I didn't realize the script shows the PID of the creating process. Change line 14 to

 Wscript.Echo "  Process ID: " & objItem.IDProcess

and you get the correct PID.

2. I thought your problem is to determine the PID of the hanging process out of multiple processes with the same name. I was hoping the script will show ehich of the wrun32 processes consumes your cpu.
Next step would be to use the PID with the highest CPU to kill the process.
0
 
reddredsonAuthor Commented:
ok so here is what i came up with so far

==========================================

Dim vbShell
 
Dim var1

DIM var2

Dim batchFile
 
Set vbShell = CreateObject("WScript.Shell")

strComputer = "."

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

set objRefresher = CreateObject("WbemScripting.SWbemRefresher")

Set colItems = objRefresher.AddEnum _
 (objWMIService, "Win32_PerfFormattedData_PerfProc_Process").objectSet
objRefresher.Refresh
 
  Wscript.Echo Now
  For Each objItem in colItems
   if Instr(objItem.name,"wrun32") > 0 then
    objRefresher.Refresh
Wscript.Echo vbCrLf & "Name: " & objItem.Name
Wscript.Echo "  Process ID: " & objItem.IDProcess
Wscript.Echo "  Percent Processor Time: " & _
      objItem.PercentProcessorTime
   
var1 = objItem.IDProcess

quotedVar1 = " " & """" & var1 & """"

var2 = objItem.PercentProcessorTime
 
quotedVar2 = " " & """" & var2 & """"

batchFile = "C:\Users\Username\Desktop\batfile1.bat"
 
vbShell.run batchFile & quotedVar1 & quotedVar2
 
set vbShell=nothing

 End if
  Next

================================================

So it finds the process, gives me correct PID and process utilization and passes the values as variable to a batch file. Thank you so much so far. but is there a query i can have it check by.  Something like:

Set colProcesses = objWMIService.ExecQuery("SELECT * FROM Win32_Process") | where objItem.PercentProcessorTime > 20

Whould that work ?
0
 
reddredsonAuthor Commented:
Ok so i came up with an easier solution. I just did a Query in vb that checks for the process and kills it.

SET wmi = GetObject("winmgmts://./root/cimv2")

perfQuery = "SELECT * FROM Win32_PerfFormattedData_PerfProc_Process " & _
            "WHERE PercentProcessorTime >= 25"
For Each p In wmi.ExecQuery(perfQuery)
  pidQuery = "SELECT * FROM Win32_Process WHERE ProcessId = " & p.IDProcess
  For Each p1 In wmi.ExecQuery(pidQuery)
    If p1.Name = "wrun32.exe" Then p1.Terminate
  Next
Next


Thanks for the help.
0
 
reddredsonAuthor Commented:
I figured out a simpler solution to my issue and wanted the answerd question availiable for others who may get the same problem.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now