Sign in to follow this  
Followers 0
Fredricz

Script eating memory by the hour...?!

5 posts in this topic

Hi, I've got a autoit script on my server that checks a lot of services (windows 7). But it eats memory, a lot. After about an hour I need to restart the script. I'm sure it's pretty easy, but I can't really see the problem here ^^

$servicename = "Example"
$stdout = Run('sc.exe query ' & $servicename, '', @SW_HIDE, 2)
    Local $data
    While 1
        $data = StdOutRead($stdout)
        If @error Then Return 'unknown'
        If $data Then
            If StringInStr($data, 'Running') Then $data = 'Running'
            If StringInStr($data, 'Stopped') Then $data = 'Stopped'
           ExitLoop
        EndIf
     WEnd

Anyone got a hint?

Share this post


Link to post
Share on other sites

Give this a try...

ConsoleWrite(_Check_Service("hidserv") & @CRLF)

Func _Check_Service($servicename)
    Local $sReturn = "Unknown", $sStdData
    Local $iPID = Run("sc.exe query " & $servicename, "", @SW_HIDE, 2)
    If Not $iPID Then Return "Process not started"
    Local $iTimer = TimerInit()
    While 1
        $sStdData &= StdoutRead($iPID)
        If @error Then ExitLoop
        If $sStdData Then
            If StringInStr($sStdData, "Running", 2) Then
                $sReturn = "Running"
                ExitLoop
            ElseIf StringInStr($sStdData, "Stopped", 2) Then
                $sReturn = "Stopped"
                ExitLoop
            ElseIf StringInStr($sStdData, "1060", 2) Then
                $sReturn = "Servicename not found"
                ExitLoop
            EndIf
        EndIf
        If TimerDiff($iTimer) > 10000 Then
            $sReturn = "Time-Out"
            ExitLoop
        EndIf
    WEnd
    ; ConsoleWrite($sStdData & @crlf)
    StdioClose($iPID)
    ProcessClose($iPID)
    Return $sReturn
EndFunc   ;==>_Check_Service

Share this post


Link to post
Share on other sites

Give this a try...

ConsoleWrite(_Check_Service("hidserv") & @CRLF)

Func _Check_Service($servicename)
Local $sReturn = "Unknown", $sStdData
Local $iPID = Run("sc.exe query " & $servicename, "", @SW_HIDE, 2)
If Not $iPID Then Return "Process not started"
Local $iTimer = TimerInit()
While 1
$sStdData &= StdoutRead($iPID)
If @error Then ExitLoop
If $sStdData Then
If StringInStr($sStdData, "Running", 2) Then
$sReturn = "Running"
ExitLoop
ElseIf StringInStr($sStdData, "Stopped", 2) Then
$sReturn = "Stopped"
ExitLoop
ElseIf StringInStr($sStdData, "1060", 2) Then
$sReturn = "Servicename not found"
ExitLoop
EndIf
EndIf
If TimerDiff($iTimer) > 10000 Then
$sReturn = "Time-Out"
ExitLoop
EndIf
WEnd
; ConsoleWrite($sStdData & @crlf)
StdioClose($iPID)
ProcessClose($iPID)
Return $sReturn
EndFunc ;==>_Check_Service

Oh, that work liked a charm. Thanks for the help, you made it better ^^. Closing the process made it work, embarrassed that I didn't think of that before.

Thanks,

Fredricz

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Glad it worked out :). Generally the process closes by itself when you read until the end of StdOutRead(). But you've exited the loop per-maturely on keyword found, leaving content in the pipe. Thus the real difference is StdioClose(), which forces the pipe to close, the ProcessClose() is just an additional pre-caution and should generally not be triggered. Oh, and I've learned this the hard way too :lol:, a script of mine was leaking memory for ages until I found out..

ConsoleWrite(_Check_Service("hidserv") & @CRLF)

Func _Check_Service($servicename)
    Local $sReturn = "Unknown", $sStdData
    Local $iPID = Run("sc.exe query " & $servicename, "", @SW_HIDE, 2)
    If Not $iPID Then Return "Process not started"
    Local $iTimer = TimerInit()
    While 1
        $sStdData &= StdoutRead($iPID)
        If @error Then ExitLoop
        If $sStdData Then
            If StringInStr($sStdData, "Running", 2) Then
                $sReturn = "Running"
                ExitLoop
            ElseIf StringInStr($sStdData, "Stopped", 2) Then
                $sReturn = "Stopped"
                ExitLoop
            ElseIf StringInStr($sStdData, "1060", 2) Then
                $sReturn = "Servicename not found"
                ExitLoop
            EndIf
        EndIf
        If TimerDiff($iTimer) > 10000 Then
            $sReturn = "Time-Out"
            ExitLoop
        EndIf
    WEnd
    ConsoleWrite(ProcessExists($iPID) & @CRLF)
    StdioClose($iPID)
    ConsoleWrite(ProcessExists($iPID) & @CRLF)
    ProcessClose($iPID)
    Return $sReturn
EndFunc   ;==>_Check_Service

ConsoleWrite(_Check_Service("hidserv") & @CRLF)

Func _Check_Service($servicename)
    Local $sReturn = "Unknown", $sStdData
    Local $iPID = Run("sc.exe query " & $servicename, "", @SW_HIDE, 2)
    If Not $iPID Then Return "Process not started"
    Local $iTimer = TimerInit()
    While 1
        $sStdData &= StdoutRead($iPID)
        If @error Then ExitLoop
        If $sStdData Then
            If StringInStr($sStdData, "Running", 2) Then
                $sReturn = "Running"
                ;ExitLoop
            ElseIf StringInStr($sStdData, "Stopped", 2) Then
                $sReturn = "Stopped"
                ;ExitLoop
            ElseIf StringInStr($sStdData, "1060", 2) Then
                $sReturn = "Servicename not found"
                ;ExitLoop
            EndIf
        EndIf
        If TimerDiff($iTimer) > 10000 Then
            $sReturn = "Time-Out"
            ExitLoop
        EndIf
    WEnd
    ConsoleWrite(ProcessExists($iPID) & @CRLF)
    StdioClose($iPID)
    ConsoleWrite(ProcessExists($iPID) & @CRLF)
    ProcessClose($iPID)
    Return $sReturn
EndFunc   ;==>_Check_Service
Edited by KaFu

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