Shell script loop runs out of memory

Thu May 31 18:16:00 GMT 2012

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 

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:


echo "Reading hash for $FILE_TO_CHECK with md5sum"
MD5PRINT=`md5sum $FILE_TO_CHECK | cut -d " " -f1`


while [[ 1 = 1 ]]
        echo "Waiting for file to change..."

        while [[ "$MD5PRINT" = "$MD5PRINTNEW" ]]
                sleep 1

                MD5PRINTNEW=`md5sum $FILE_TO_CHECK | cut -d " " -f1`

        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`


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 


Problem reports:
Unsubscribe info:

More information about the Cygwin mailing list