Shell script loop runs out of memory

Jordan uptownb0y@yahoo.com
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 
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



More information about the Cygwin mailing list