X-Recipient: archive-cygwin AT delorie DOT com X-SWARE-Spam-Status: No, hits=1.8 required=5.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED,FORGED_YAHOO_RCVD,FREEMAIL_FROM,NML_ADSP_CUSTOM_MED,RCVD_NUMERIC_HELO,SPF_HELO_PASS,T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org To: cygwin AT cygwin DOT com From: Jordan Subject: Shell script loop runs out of memory Date: Thu, 31 May 2012 17:42:30 +0000 (UTC) Lines: 68 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit User-Agent: Loom/3.14 (http://gmane.org/) X-IsSubscribed: yes Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT cygwin DOT com Mail-Followup-To: cygwin AT cygwin DOT com Delivered-To: mailing list cygwin AT cygwin DOT com Hi folks, I've written a shell script running under CygWin, the purpose of which is to monitor a file for changes. If the MD5 hash fails to match the previous hash, it will execute a command to process the file. I used a 1-second delay between checks of the hash. This works great for several hours, but then gives an "out of memory" error and actually brings Windows 7 to its knees. The script uses a loop within a loop; the outer loop is infinite by design, and the inner loop ends when it finds a non-matching hash and processes the file. It broke while running the inner loop, without the file having been modified at that point in time. The file was modified numerous times previously, triggering the code below the inner loop, but not around the time when the memory error occurred. I am just wondering why the loops here are consuming increasing amounts of memory over time? I'm assigning new MD5 values into existing variables over and over, not allocating new variables for each MD5 assignment. (Right??) Is 1 second perhaps too short a delay... does the system need time to deallocate something between each iteration of the inner loop? Here is the script: ------------------------ #!/bin/sh FILE_TO_CHECK=/mypath/style.less echo "Reading hash for $FILE_TO_CHECK with md5sum" MD5PRINT=`md5sum $FILE_TO_CHECK | cut -d " " -f1` MD5PRINTNEW=$MD5PRINT while [[ 1 = 1 ]] do echo "Waiting for file to change..." while [[ "$MD5PRINT" = "$MD5PRINTNEW" ]] do sleep 1 MD5PRINTNEW=`md5sum $FILE_TO_CHECK | cut -d " " -f1` done echo "File was modified ... Running compiler..." /mypath/lessc $FILE_TO_CHECK /mypath/style.css -x echo "Reading hash for $FILE_TO_CHECK with md5sum" MD5PRINT=`md5sum $FILE_TO_CHECK | cut -d " " -f1` MD5PRINTNEW=$MD5PRINT done ------------------------ Any help would be appreciated. I can provide the exact memory error if requested, but I would need some help to know which logs (if any) in CygWin to look at, to dig around and find the error text. (I'd rather not run it all day to reproduce the error again. The error was definitely something related to my CygWin shell running out of memory.) (If you propose a solution which involves increasing the memory available to CygWin, that seems illogical, because the script is gradually increasing its own memory usage over time. Thus, such a solution would only delay the inevitable, I think.) Thanks! Jordan -- Problem reports: http://cygwin.com/problems.html FAQ: http://cygwin.com/faq/ Documentation: http://cygwin.com/docs.html Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple