Stuball2 Posted February 6, 2013 Share Posted February 6, 2013 (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 February 6, 2013 by Stuball2 Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 6, 2013 Moderators Share Posted February 6, 2013 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? M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Stuball2 Posted February 6, 2013 Author Share Posted February 6, 2013 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. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 6, 2013 Moderators Share Posted February 6, 2013 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 ;==>GetCPUIn 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 Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Stuball2 Posted February 6, 2013 Author Share Posted February 6, 2013 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. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now