Solved

Tasklisk and taskkill bats

Posted on 2013-06-17
12
1,217 Views
Last Modified: 2013-06-30
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
Comment
Question by:reddredson
  • 7
  • 5
12 Comments
 
LVL 28

Expert Comment

by:Michael Pfister
Comment Utility
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
 

Author Comment

by:reddredson
Comment Utility
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
 
LVL 28

Expert Comment

by:Michael Pfister
Comment Utility
Can the VBScript help you to find processes consuming the CPU?
0
 

Author Comment

by:reddredson
Comment Utility
The script does show the running processes but i already know the process name. Its "wrun32.exe *32".
0
 
LVL 28

Expert Comment

by:Michael Pfister
Comment Utility
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
 

Author Comment

by:reddredson
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 28

Expert Comment

by:Michael Pfister
Comment Utility
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
 

Author Comment

by:reddredson
Comment Utility
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
 
LVL 28

Expert Comment

by:Michael Pfister
Comment Utility
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
 

Author Comment

by:reddredson
Comment Utility
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
 

Accepted Solution

by:
reddredson earned 0 total points
Comment Utility
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
 

Author Closing Comment

by:reddredson
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

I use more than 1 computer in my office for various reasons. Multiple keyboards and mice take up more than just extra space, they make working a little more complicated. Using one mouse and keyboard for all of my computers makes life easier. This co…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now