Hits : 4030

Incrond :: Mini How To 



HomePage of incrond: incron


The inotify cron handles filesystem events rather than time periods.


Το incron είναι ένα πρόγραμμα παρόμοια χρήσης και λειτουργίας με το cron.
Η διαφορά του με το cron είναι ότι δεν ενεργοποιείται σε χρονικές περιόδους,
αλλά σε διάφορα συμβάντα στο σύστημα των αρχείων μας.

inotify events


Tα συμβάντα είναι τα εξής:


Event Name Is an Event Description
IN_ACCESS Yes  file was accessed.
IN_ATTRIB Yes  metadata changed.
IN_CLOSE_NOWRITE Yes  unwrittable file was closed.
IN_CLOSE_WRITE Yes  writtable file was closed.
IN_CREATE Yes  file/dir was created in watched directory.
IN_DELETE Yes  file/dir was deleted in watched directory.
IN_DELETE_SELF Yes  watched item itself was deleted.
IN_DONT_FOLLOW No  don't follow a symlink (lk 2.6.15).
IN_IGNORED Yes  raised on watched item removing. Probably useless for you, prefer instead IN_DELETE*.
IN_ISDIR No  event occurred against directory. It is always piggybacked to an event. The Event structure automatically provide this information (via .is_dir)
IN_MASK_ADD No  to update a mask without overwriting the previous value (lk 2.6.14). Useful when updating a watch.
IN_MODIFY Yes  file was modified.
IN_MOVE_SELF Yes  watched item itself was moved, currently its full pathname destination can only be traced if its source directory and destination directory are both watched. Otherwise, the file is still being watched but you cannot rely anymore on the given path (.path)
IN_MOVED_FROM Yes  file/dir in a watched dir was moved from X. Can trace the full move of an item when IN_MOVED_TO is available too, in this case if the moved item is itself watched, its path will be updated (see IN_MOVE_SELF).
IN_MOVED_TO Yes  file/dir was moved to Y in a watched dir (see IN_MOVE_FROM).
IN_ONLYDIR No  only watch the path if it is a directory (lk 2.6.15). Usable when calling .add_watch.
IN_OPEN Yes  file was opened.
IN_Q_OVERFLOW Yes  event's queue overflow. This event doesn't belongs to any particular watch.
IN_UNMOUNT Yes  backing fs was unmounted. Notified to all watches located on this fs.

top


Απαιτήσεις


Χρειάζεται η υποστήριξη του inotify στον πυρήνα μας, γεγονός που υλοποιήθηκε στην έκδοση 2.6.13
Οπότε οποιαδήποτε νεότερη έκδοση μας κάνει.


* Linux kernel 2.6.13 or later


$ ls -l /usr/include/sys/inotify.h
$ ls -l /usr/include/linux/inotify.h


top


Εγκατάσταση


Η εγκατάσταση του προγράμματος incrond (inotify cron daemon) είναι εξαιρετικά απλή:


$ wget -c http://inotify.aiken.cz/download/incron/incron-0.5.8.tar.bz2
$ tar jxvf incron-0.5.8.tar.bz2
$ cd incron-0.5.8
$ make
$ su -
# make install


Μετά την ολοκλήρωση της εγκατάστασης χρειάζεται να δημιουργήσουμε το αρχείο ρυθμίσεων
του προγράμματος με την εξής εντολή:


# cp /etc/incron.conf.example /etc/incron.conf


Για να εκκινήσουμε το πρόγραμμα αρκεί:


# /usr/local/sbin/incrond


Βλέπουμε ότι τρέχει:


# ps -ef | grep in[c]ron
root     15917     1  0 21:11 ?        00:00:00 /usr/local/sbin/incrond


top


Χρήση


Το λογισμικό incrond έχει παρόμοια λειτουργία με το crond.
Η σύνταξή του είναι η εξής:


<path> <mask> <command>


Για να γίνει πιο κατανοητό ακολουθεί ένα παράδειγμα χρήσης.
Θέλουμε να μας εμφανίζεται ένα μήνυμα στην οθόνη μας κάθε φορά που κάποιος χρήστης διαβάζει τα περιεχόμενα ενός αρχείου:


Δημιουργούμε το αρχείο μας,


$ date > file


Εισάγουμε ένα entry στον δαίμονα inotify,


$ export EDITOR=vi
$ incrontab -e


Και γράφουμε το εξής:


/home/ebal/file IN_ACCESS /usr/bin/zenity --info --text "Κάποιος διαβάζει τα αρχεία μου !" --display=:0


Επιβεβαιώνουμε ως εξής:


$ incrontab -l
/home/ebal/file IN_ACCESS zenity --info --text I\ 'll\ kill\ you --display=:0


ΠΡΟΣΟΧΗ: Το incrontab ΔΕΝ είναι ακόμα ώριμο να διαβάζει πλήρως μία εντολή ή ένα πρόγραμμα όπως το cron


οπότε όποιος διαβάζει το αρχείο μας:


$ cat file
Δευ 12 Ιαν 2009 10:14:13 μμ EET


Εμφανίζεται το εξής ωραίο μήνυμα:


file:incrond_example.jpeg


top


Χρήσιμα Urls


http://en.wikipedia.org/wiki/Inotify
http://www.kernel.org/pub/linu[..]e/rml/inotify/README
http://www.linuxjournal.com/article/8478


top


pyinotify


Νομίζω η πληρέστατη βιβλιοθήκη inotify, γραμμένη σε python


http://trac.dbzteam.org/pyinotify/wiki


python -m pyinotify -rv /tmp/data


top