Fredricz Posted March 23, 2013 Posted March 23, 2013 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?
JohnOne Posted March 23, 2013 Posted March 23, 2013 If all that is in another loop, you might want to consider ending the process "sc.exe" AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans.
KaFu Posted March 23, 2013 Posted March 23, 2013 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 OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2024-Oct-13) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Oct-13) HMW - Hide my Windows (2024-Oct-19) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2025-May-18) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16)
Fredricz Posted March 24, 2013 Author Posted March 24, 2013 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
KaFu Posted March 24, 2013 Posted March 24, 2013 (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 , 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 March 24, 2013 by KaFu OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2024-Oct-13) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Oct-13) HMW - Hide my Windows (2024-Oct-19) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2025-May-18) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16)
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