Jump to content

Running a set number of apps at once


Recommended Posts

Hey All,

I was wondering if anyone might have any suggestions for a script I'm trying to improve at work.

Long story short I have Script A that will ping a certain range of IP addresses if a ping is successful it then runs the remote execuatable program psexec to that IP address which copies another AutoIT script (Script B ) that does some auditing information on that machine and then copies the .txt files it generates back to the workstation Script A was run from. For the most part this works great however for some reason psexec sometimes seems to just sit there doing nothing and the script will continue to wait for psexec to finish before continuing to audit the next machine (i was originally using RunWait() on psexec with a For loop, looping through each machine).

I was hoping to come up with a way where I could instead open several instances of psexec (say 8 at once) using Run() and then let the script sit there till less than 8 instances of psexec exist and if less than 8 exist start auditing another machine. Ideally if a process has existed for to long that process should be terminated freeing up another spot for another psexec process to start up.

So far i've got rough code that is trying to create an array of PID's for each instance and pause it if the number of elements are greater than 3 (3 for testing) but it keeps crashing out with array subscripts dimensions exceeded and I think it's because im either trying to delete an element (a PID that has finished) that isn't there or loop through the wrong number of elements but I'm not to sure.

I'm pretty sure i might be turning this into something that is harder than it should be so if anyone has any ideas on how to do this more simply/efficiently it would be greatly appreciated.

Here's what I have so far (relevant stuff):

For $i = 1 To 8 ;will ping IP address from 1 to 8 in the given subnet
    $pingtest = Ping($subnet & "." & $i, 200) ;pings the subnet and value of $i which will be each host IP to try, and will wait 200 milliseconds for a response
    If $pingtest Then ;means the ping was successful
        
        
        $localPID = Run("C:\Audit\psexec.exe \\" & $subnet & "." & $i & " -u " & $SOE2user & " -p " & $SOE2pass & " -n 90 -c C:\Audit\localaudit.exe " & @IPAddress1 & " " & $subnet)

        _ArrayAdd($arrayPID, $localPID)
        
        $test = UBound($arrayPID) - 1
        
        While $test > 3
            Sleep(3000)
            For $x = 1 To UBound($arrayPID) - 1
                If ProcessExists($arrayPID[$x]) Then
                    ConsoleWrite($arrayPID[$x] & " - Exists" & @LF)
                Else
                    ConsoleWrite($arrayPID[$x] & " - Doesnt Exist" & @LF)
                    _ArrayDelete($arrayPID, $x)
                EndIf
            Next
            
        WEnd
        
    EndIf
Next ;goes and tries the next IP address
Link to comment
Share on other sites

but it keeps crashing out with array subscripts dimensions exceeded and I think it's because im either trying to delete an element (a PID that has finished) that isn't there or loop through the wrong number of elements but I'm not to sure.

Would it help if you iterated through the array in reverse?
Link to comment
Share on other sites

Would it help if you iterated through the array in reverse?

Hmm think i may be getting a bit closer the following code will keep 3 instances of notepad open (as long as they aren't closed to fast). I think one of the problems may have been I never updated the test condition in the loop.

I've never had to iterate through an array backwards before. How would it help me out here? I'm guessing it just works the same way but i would start the loop at Ubound($arrayPID) - 1 and the step would be -1?

#include <Array.au3>

Dim $arrayPID[1]

For $i = 1 To 10

        $localPID = Run("notepad.exe")
        ;ConsoleWrite($localPID & @LF)
        _ArrayAdd($arrayPID, $localPID)
        
        $test = UBound($arrayPID) - 1
        
        ConsoleWrite($test & @LF)
        
        While $test >= 3
            Sleep(1000)
            For $x = 1 To UBound($arrayPID) - 1
                If ProcessExists($arrayPID[$x]) Then
                    ;ConsoleWrite($arrayPID[$x] & " - Exists" & @LF)
                Else
                    ;ConsoleWrite($arrayPID[$x] & " - Doesnt Exist" & @LF)
                    _ArrayDelete($arrayPID, $x)
                EndIf
            Next
            $test = UBound($arrayPID) - 1
        WEnd
        

Next
Link to comment
Share on other sites

How about this way (seems easier to me):

HotKeySet("{ESC}", "_Quit")

; Keep 3 instances of notepad running
While 1
    $avPID = ProcessList("notepad.exe")
    If $avPID[0][0] < 3 Then Run("notepad.exe")
    Sleep(200)
WEnd

Func _Quit()
    Exit
EndFunc

:rolleyes:

Edit: Didn't need the #include <array.au3> in there, it was left over from using _ArrayDisplay() as a debug message.

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

How about this way (seems easier to me):

#include <array.au3>

HotKeySet("{ESC}", "_Quit")

; Keep 3 instances of notepad running
While 1
    $avPID = ProcessList("notepad.exe")
    If $avPID[0][0] < 3 Then Run("notepad.exe")
    Sleep(200)
WEnd

Func _Quit()
    Exit
EndFunc

:rolleyes:

Thanks I overlooked ProcessList this seems to be a much better way of doing it! Now i just gotta work out a way of tracking how long each process has been open for and closing it if it goes over a certain time. Hopefully should get a chance to play with this a bit more today. :rambo:
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...