Barfly Posted February 27, 2007 Share Posted February 27, 2007 I am currently running Selenium core through ant and it annoyingly leaves a java.exe process running if the script fails. I then have to open Task manager and compare the two java.exe processes that are running and manually kill the smaller one. I love Autoit and thought that maybe I could create a small script that would do this for me manually, there just does not seem to be an easy way. I have gone through the forum and seen some suggestions, but nothing seems to work for me (unless I am being stupid??). So basically want I want to do is... CODE $list = ProcessList("java.exe") If $list[0][0] = 1 Then ;Only one instance of java.exe running so exit script Exit ElseIf $list[0][0] = 0 Then ;No java.exe running, exit script Exit Else Do $list = ProcessList("java.exe") ; This is where I want to be able to compare which of them is using the most memory, at the moment I am comparing which one has the smallest ProcessID (not ideal) If $list[1][1] < $list[2][1] Then $id = $list[1][1] Else $id = $list[2][1] EndIF ProcessClose($id) Until $list[0][0] = 1 EndIf I do not mind if the answer to this is long winded, but surely it is not that hard???? Link to comment Share on other sites More sharing options...
HardHackz Posted February 27, 2007 Share Posted February 27, 2007 (edited) Hmm...well, when I try to run the script with two mspaint.exe's it seems to work but I also get this error message in SciTE: C:\Documents and Settings\Compaq_Owner\Desktop\test.au3 (11) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.: If $list[1][1] < $list[2][1] Then If $list[1][1] < ^ ERROR So it seems like something is wrong with the variables, I'm a n00b to AutoIT though so correct me if I'm wrong. But it did close one of my paints. Also, like I said, I'm a n00b but what is the greater thing it's looking for? I don't see you tell it to look at the amount of CPU taken up or anything. Edited February 27, 2007 by HardHackz [center]My BlogOldSock Programs (AutoIT Apps)A Message to Hackers! (NOT SKIDDIES!)OldSock Programs is my little "company."[/center] Link to comment Share on other sites More sharing options...
Barfly Posted February 27, 2007 Author Share Posted February 27, 2007 Thanks, I appreciate you trying this. I know that the script works and that it throws an error. At the moment it is comparing the two processes to see which one has the biggest ProcessID, which makes no sense really. I would like to change this bit of my code with some code that gets and compares the Mem Usuage where I compare the Process size. PS - I didn't fix the error yet, as the script is not doing what I want yet. Link to comment Share on other sites More sharing options...
Barfly Posted February 28, 2007 Author Share Posted February 28, 2007 OK, I have found some help buried deep in the forum...w0uter's memory functions, but I cannot get it to give me what I want....OK, I am being really silly here or what? I can get the code to return a value from $v_Read, but what is it returning exactly it just looks like a number and is not the mem usuage figure I was after? I just want it to return what it states in Task Manager eg: 1,687k or 16,000k in the Mem Usuage column. What am I doing wrong? Link to comment Share on other sites More sharing options...
jefhal Posted February 28, 2007 Share Posted February 28, 2007 (edited) Here's a script I put together for you that kills the larger of any number of processes with name $application. It uses Powershell to find the processes and return their working set size and process ids: #include <Constants.au3> #include <Array.au3> Dim $chunk Dim $application = "notepad" Opt("TrayIconDebug",1) HotKeySet("{ESC}","_terminate") $foo = Run('\\server\path\powershell.exe "gps -name ' & $application & ' | ft -h -a ws,id"',@TempDir,@SW_HIDE,$stdout_child + $stdin_child) StdinWrite($foo) While 1 $chunk &= StdoutRead($foo) If @error Then ExitLoop Wend $arProcessIDSZ = StringSplit($chunk,@CRLF) _ArraySort($arProcessIDSZ,1,1) For $i = UBound($arProcessIDSZ) -1 to 1 Step -1 If $arProcessIDSZ[$i] = "" Then _ArrayPop($arProcessIDSZ) ReDim $arProcessIDSZ[$i] $arProcessIDSZ[0] = $i Else EndIf Next _ArrayDisplay($arProcessIDSZ,"$arProcessIDSZ") $max = _ArrayMax($arProcessIDSZ) $pid = StringTrimLeft($max,StringInStr($max," ")) MsgBox(1,"Warning","Killing " & $application & " with process ID " & $pid & " now...") ;ProcessClose($pid) Func _terminate() Exit 0 EndFunc Edited February 28, 2007 by jefhal ...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format Link to comment Share on other sites More sharing options...
Barfly Posted March 2, 2007 Author Share Posted March 2, 2007 Here's a script I put together for you that kills the larger of any number of processes with name $application. It uses Powershell to find the processes and return their working set size and process ids: #include <Constants.au3> #include <Array.au3> Dim $chunk Dim $application = "notepad" Opt("TrayIconDebug",1) HotKeySet("{ESC}","_terminate") $foo = Run('\\server\path\powershell.exe "gps -name ' & $application & ' | ft -h -a ws,id"',@TempDir,@SW_HIDE,$stdout_child + $stdin_child) StdinWrite($foo) While 1 $chunk &= StdoutRead($foo) If @error Then ExitLoop Wend $arProcessIDSZ = StringSplit($chunk,@CRLF) _ArraySort($arProcessIDSZ,1,1) For $i = UBound($arProcessIDSZ) -1 to 1 Step -1 If $arProcessIDSZ[$i] = "" Then _ArrayPop($arProcessIDSZ) ReDim $arProcessIDSZ[$i] $arProcessIDSZ[0] = $i Else EndIf Next _ArrayDisplay($arProcessIDSZ,"$arProcessIDSZ") $max = _ArrayMax($arProcessIDSZ) $pid = StringTrimLeft($max,StringInStr($max," ")) MsgBox(1,"Warning","Killing " & $application & " with process ID " & $pid & " now...") ;ProcessClose($pid) Func _terminate() Exit 0 EndFunc Thanks for writing this for me JefHal. It does seem to work though. I am go to try and mofify it so that it works for me. Thanks again! Link to comment Share on other sites More sharing options...
Barfly Posted March 2, 2007 Author Share Posted March 2, 2007 :"> Could somone just show me which variable in JefHal's script is the Mem Usuage size? That way I cam compare them? Or where in thwe script that the comparison is done? I cannot get my head around the commands he is using and they are not in the help file. :"> Link to comment Share on other sites More sharing options...
nitekram Posted March 2, 2007 Share Posted March 2, 2007 :"> Could somone just show me which variable in JefHal's script is the Mem Usuage size? That way I cam compare them? Or where in thwe script that the comparison is done? I cannot get my head around the commands he is using and they are not in the help file. :">maybe look on google for powershell.exe 2¢ All by me:"Sometimes you have to go back to where you started, to get to where you want to go." "Everybody catches up with everyone, eventually" "As you teach others, you are really teaching yourself." From my dad "Do not worry about yesterday, as the only thing that you can control is tomorrow." WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2 AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit Docs SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language Programming Tips Excel Changes ControlHover.UDF GDI_Plus Draw_On_Screen GDI Basics GDI_More_Basics GDI Rotate GDI Graph GDI CheckExistingItems GDI Trajectory Replace $ghGDIPDll with $__g_hGDIPDll DLL 101? Array via Object GDI Swimlane GDI Plus French 101 Site GDI Examples UEZ GDI Basic Clock GDI Detection Ternary operator Link to comment Share on other sites More sharing options...
jefhal Posted March 2, 2007 Share Posted March 2, 2007 :"> Could somone just show me which variable in JefHal's script is the Mem Usuage size? That way I cam compare them? Or where in thwe script that the comparison is done? I cannot get my head around the commands he is using and they are not in the help file. :">Let me try to decipher the powershell syntax. I like it because it takes up less room than wmi code, but it can be daunting to a new viewer: powershell.exe "gps -name ' & $application & ' | ft -h -a ws,id" gps => get-process (gets a list of all processes running on a computer) & $application & => inserts the AutoIT variable that contains the name of the process that you are looking for (e.g. java) | ft => pipe to the format-table commandlet -h => hide the headers (less cleanup required) -a => automatic formatting (removes spaces before and after the output) ws,id => only include the workingsetsize and the processid in the output table All of the lines below the run statement are required to get the output of the powershell shell into AutoIT. The output of the above powershell command line is a table of only the size and the process id for all processes named 'java' that are running on the target machine. The line without aliases is:powershell.exe get-process -name java | format-table -hide -auto ws,id Hope this helps... ...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format 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