Question about app or script to monitor changes

Posted on 2006-06-03
Last Modified: 2010-04-21
I wrote a bash script that reads a file, does a bunch of stuff then writes a value into mysql.
We then made this script loop and have it load on startup in a cron job.

Is there a better way to do this?  Basically I have a dir, where the files are being constantly updated - and what I would like to know is when the file changes. Then write to db.  Is there a better way to program this?  Any recommendations?  

Right now, I just keep reading the file, and if its different than what I have in my db, I write to log and update current value.

Looking for suggestions.  Just as a side note -- we had a app that would load current value into a text box, then when the text box would change -- to do what it needed to do.  Looking to do something compareable in Linux.  We do have it working with bash and mysql right now... seeing if there is a better way.

Question by:Tigger996
    LVL 34

    Accepted Solution

    I'm not aware of any way a process can be notified that the content of a file has changed. All methods I can think of require some kind of polling. You could poll the file's date-time-modified (DTM). AFAIK, that's updated whenever the file is written to. Or you could md5sum the file, and only re-process it when the sum changes. That's a much lower processing cost than reading and processing with bash, so you could afford to do it more often. Any shell pipeline I can think of right now to even extract DTM would cost more than the md5sum, unless the file is large.
    LVL 43

    Assisted Solution

    LVL 5

    Assisted Solution

    What is writing to the files in the directory that is constantly being updated?  You could throw a wrapper on that program/process to have it make the change to the database for you, or even back up the file in another directory and save the last x number of changes.  I do something similar with vim.  Everytime I open a file to editing with vim, vim gets called through a function in my bash profile that creates a backup and stores the last 10 changes for me.  Just a thought.  

    LVL 43

    Expert Comment

    The only reasonable and safe and fase (supported by the kernel) is the gamin(FAM does polling). It should not be split...
    LVL 20

    Expert Comment

    The three comments are giving good information.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
    The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now