Linux uses inotify-tools's inotifywait implementation: When the folder content changes, a script is automatically executed.

Source: Internet
Author: User
Tags inotify egrep

Linux uses inotify-tools's inotifywait implementation: When the folder content changes, a script is automatically executed. When I create an rpm package management server, this requirement exists, when a new rpm is stored in a specified directory, a script is automatically executed to check the rpm package. The inotifywait module of inotify-tools is used here. There is an event processing parameter-e, which can be found in its Manual. My code is as follows: [python] #/bin/bash ################################ ################################# automatically run a script (action. sh) when the contents # of a directory ($ {EVENTPATH}) changed. # pls. install the inotify-tools-3.13-1.el4.rf.i386.rpm module # before use this scripts # Aborn Jiang (aborn.jiang@gmail.com) # Sep.8, 2013 ####################################### ######################### EV ENTPATH = ". "MSG = ". inotifymsg "PATTERN = ". rpm $ "# only when the rpm files changed. while inotifywait-e modify-e create-e delete-e moved_to-e moved_from \$ {EVENTPATH} 1 >1 {EVENTPATH}/$ {MSG} 2>/dev/null; do FILE = 'cat $ EVENTPATH/$ {MSG} | egrep $ {PATTERN} | awk '{print $3}' 'Action = 'cat $ EVENTPATH/$ {MSG} | egrep $ {PATTERN} | awk '{print $2 }''[! -Z $ {FILE}] & \ echo "in the directory $ {EVENTPATH}, the file :$ {FILE} modified, action :$ {ACTION }"&&\. /action. sh done I only want to check whether there is any rpm change here. You can modify the PATTERN variable content in the code to detect any file! If you want to detect any file, you can set PATTERN to "*" and put the execution script in action. sh.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.