Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Tasklisk and taskkill bats

Posted on 2013-06-17
12
Medium Priority
?
1,282 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 29

Expert Comment

by:Michael Pfister
ID: 39255843
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
ID: 39255980
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 29

Expert Comment

by:Michael Pfister
ID: 39256279
Can the VBScript help you to find processes consuming the CPU?
0
Protect Your Retail Business and Reputation

Wi-Fi access doesn't just impact your business & customer experience, it can also affect your security.  Join us for an informative webinar to learn more about the top threats and trends impacting retail today, and the key solutions to protecting retail networks and reputations.

 

Author Comment

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

Expert Comment

by:Michael Pfister
ID: 39256703
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
ID: 39257511
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
 
LVL 29

Expert Comment

by:Michael Pfister
ID: 39258625
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
ID: 39260651
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 29

Expert Comment

by:Michael Pfister
ID: 39261725
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
ID: 39264050
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
ID: 39266880
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
ID: 39287911
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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We take a look at some of the most common obstacles that IT teams run into as they work relentlessly to keep all the alarms and sirens from going off at once.
New style of hardware planning for Microsoft Exchange server.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Progress

670 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