Sign in to follow this  
Followers 0
Stuball2

Kill Process if it's CPU drops below n%

5 posts in this topic

#1 ·  Posted (edited)

Hi All,

Firstly I am aware that this has been posted and answered before so I apologise in advance - I know how annoying it is repeating yourself but I am stumped, I have read all the posts I could find but really could not figure out how to implement the solutions posted.

Also this is my first foray into AutoIt so maybe I am missing the obvious?

So my script at the moment is like this, essentially I restart the process every 10 minutes cause it bums out, relaunch and press some buttons (to set it up again).

While 1
Run("process.exe")
WinWaitActive("Window Name")
Send("{RIGHT}")
Send("{RIGHT}")
Sleep(2000)
Send("{TAB}")
Sleep(2000)
Send("{SPACE}")
Sleep(600000)
Run("pskill process.exe")
Sleep(5000)
WEnd

But, it doesnt bum out every 10 minutes, sometimes it bums out after 5 minutes and other times it could run for 4 hours+ so my method isn't very good really.

When it does bum out, the process continues but CPU drops to less than 5% for the process.exe

So I would like to implement a check every 30 secs for current CPU and if its less than say 20% then pskill process.exe and loop to the beggining.

So googling and searching etc led me to this but I don't understand scripting too well:

$process = "scite"
While 1
Sleep(10)
$cpu_usage = _Get_CPU_Usage(@ComputerName, 5, $process); return usages if above the percent passed in
If Not @error Then
     MsgBox(0, "CPU Usage of " & $process, $cpu_usage)
     ExitLoop
EndIf
WEnd
Func _Get_CPU_Usage($strComputer, $percent = 5, $strProcess = "")
$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""

If $strProcess <> "" Then $strProcess = " WHERE Name = '" & $strProcess & "'"
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_PerfFormattedData_PerfProc_Process" & $strProcess, "WQL", _
         $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) Then
     For $objItem In $colItems
         If Number($objItem.PercentProcessorTime) > Number($percent) Then Return $objItem.PercentProcessorTime
     Next
EndIf
SetError(1)
Return ""
EndFunc ;==>_Get_CPU_Usage

I think I am lost at: where in this script do I put process.exe and also 20% CPU? I found 1 example where the $process had been changed to taskmgr, but I am not sure why this is not taskmgr.exe?

If someone could point me in the right direction I would be very grateful.

Thanks in advance.

Edited by Stuball2

Share this post


Link to post
Share on other sites



Stuball2,

Welcome to the AutoIt forum. :)

Could you explain "burns out" a bit more clearly? And what exactly is this incendiary process that you wish to monitor? :huh:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Hi Melba,

Thanks for responding :)

What it acually is Auto13.exe, essentially it logs into a web service and pulls information, what actually happens (due to it connecting to unstable servers) is that the connection is lost.

At this point the Application stays as the active Window but stops searching, and uses very little CPU as it is essentially dormant. The app itself is not aware that it has disconnected and can't reconnect automatically. It is a just a small custom .net application (not built by me).

So my thought is that the easiest way is to re-launch the App... I am open to other ideas though.

Thanks.

Share this post


Link to post
Share on other sites

Stuball2,

Personally I use something like this to get CPU usage - it can be used for a single process or to get everything running:

#include <Array.au3>

$aProcess_Info = GetCPU("scite")

_ArrayDisplay($aProcess_Info)

Func GetCPU($sProcess = "")

    Local $aAllProcess_Info[1000][3] = [[0]], $aOneProcess_Info[3]

    $oWinMgmts = ObjGet("winmgmts:\\.\root\cimv2")
    $aCPU_Usage = $oWinMgmts.ExecQuery("SELECT Name, IDProcess, PercentProcessorTime FROM Win32_PerfFormattedData_PerfProc_Process")

    For $vCPU_Info In $aCPU_Usage
        If $sProcess Then
            If $vCPU_Info.Name = $sProcess Then
                $aOneProcess_Info[0] = $sProcess
                $aOneProcess_Info[1] = $vCPU_Info.IDProcess
                $aOneProcess_Info[2] = $vCPU_Info.PercentProcessorTime & " %"
                Return $aOneProcess_Info
            EndIf
        Else
            $aAllProcess_Info[0][0] += 1
            $aAllProcess_Info[$aAllProcess_Info[0][0]][0] = $vCPU_Info.Name
            $aAllProcess_Info[$aAllProcess_Info[0][0]][1] = $vCPU_Info.IDProcess
            $aAllProcess_Info[$aAllProcess_Info[0][0]][2] = $vCPU_Info.PercentProcessorTime & "%"
        EndIf
    Next

    ReDim $aAllProcess_Info[$aAllProcess_Info[0][0] +1][3]
    Return $aAllProcess_Info
EndFunc   ;==>GetCPU

In your case you would just need to see if $aProcess_Info[2] was less than 20% using a simple If structure - I would use AdlibRegister to check every so often. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Thanks very much,

It works... I probably have not implemented it very well but essentially I have used

run process, wait 30 seconds, if CPU is greater 25 then sleep 5 minutes else kill process, then loop.

Not nice but it works for me.

Thanks again for you help!

Stu.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0