incrond

最後更新: 2019-08-07

介紹

 * trigger commands when a file or directory event occurs

 * incrond 不支援 Recursive directory

 


Install

 

# C6, C7: epel

yum install incron

獲得 program:

  • incrond   
  • incrontab

Config:

/etc/incron.conf

 


Enable & Start Service

 

# C7

systemctl enable incrond

systemctl start incrond

# C6

chkconfig incrond on

service incrond start

 


incrontab

 

incrontab - a table manipulator for the incron system.

Permission

If /etc/incron.allow exists only users listed here may use incron

Otherwise if /etc/incron.deny exists only users NOT listed here may use incron

echo "root" > /etc/incron.allow

CMD

# list

incrontab -l

# edit (/var/spool/incron/<USERNAME>)

 * Column 不能用 Tab 分開

incrontab -e

# reloading the current table by incrond

incrontab --reload

# list of supported event types(mask) (delimited by commas)

incrontab -t

incrontab 的 Format

Help man 5 incrontab

Format

<path> <mask> <command>

path: an absolute filesystem path ( the same path may occur only once per table )

mask: an event mask (in symbolic or numeric form)

mask:

  •  IN_ACCESS
  •  IN_ATTRIB                      4
  •  IN_CLOSE_WRITE           8
  •  IN_CLOSE_NOWRITE
  •  IN_CREATE                    256
  •  IN_DELETE                     512
  •  IN_DELETE_SELF
  •  IN_MODIFY                    2
  •  IN_MOVE_SELF
  •  IN_MOVED_FROM
  •  IN_MOVED_TO
  •  IN_OPEN                        32
  •  IN_ALL_EVENTS

WILDCARDS

  • $$    dollar sign
  • $@   watched filesystem path
  • $#    event-related file name
  • $%   event flags (textually)
  • $&    event flags (numerically)

Usage:

incrontab -l

/root/test_folder     IN_ALL_EVENTS     /root/scripts/mon.sh "$@ $# $& $%"

cat /root/scripts/mon.sh

#!/bin/bash
# 這裡的 "$@" 不是 incrond 的 "$@"
echo "$@" >> /tmp/log.txt

Remark

* Path 不支援 *.txt 之類 (/root/test_folder/*.txt)

* 要 mon 的 file 要存在, 如本身不在, 那就要 "incrontab --reload" 舌知了它的建立

 

Create File

echo "test" >> test.txt

"/root/test_folder test.txt 8 IN_CLOSE_WRITE"
"/root/test_folder test.txt 256 IN_CREATE"
"/root/test_folder test.txt 32 IN_OPEN"
"/root/test_folder test.txt 2 IN_MODIFY"

echo "test" > test.txt

"/root/test_folder test.txt 8 IN_CLOSE_WRITE"
"/root/test_folder test.txt 256 IN_CREATE"
"/root/test_folder test.txt 32 IN_OPEN"
"/root/test_folder test.txt 2 IN_MODIFY"

touch test.txt

"/root/test_folder test.txt 8 IN_CLOSE_WRITE"
"/root/test_folder test.txt 256 IN_CREATE"
"/root/test_folder test.txt 32 IN_OPEN"
"/root/test_folder test.txt 4 IN_ATTRIB"

Append File

echo "test" >> test.txt

"/root/test_folder test2.txt 32 IN_OPEN"
"/root/test_folder test2.txt 2 IN_MODIFY"
"/root/test_folder test2.txt 8 IN_CLOSE_WRITE"

Touch File

"/root/test_folder test2.txt 32 IN_OPEN"
"/root/test_folder test2.txt 4 IN_ATTRIB"
"/root/test_folder test2.txt 8 IN_CLOSE_WRITE"

結論:

所有 create file 都有 IN_CLOSE_WRITE event

Delete File

"/root/test_folder test2.txt 512 IN_DELETE"

Move

# move in

"/root/test_folder test.txt 128 IN_MOVED_TO"

# move out

"/root/test_folder test.txt 64 IN_MOVED_FROM"

Read

cat test.txt

"/root/test_folder test.txt 32 IN_OPEN"
"/root/test_folder test.txt 16 IN_CLOSE_NOWRITE"

List

ls

"/root/test_folder  1073741856 IN_OPEN,IN_ISDIR"
"/root/test_folder  1073741840 IN_CLOSE_NOWRITE,IN_ISDIR"

 


設定檔: /etc/incron.conf

 

# examined by incrond for system table files
system_table_dir = /etc/incron.d

# examined by incrond for user table files
user_table_dir = /var/spool/incron

# contains users allowed to use incron
allowed_users = /etc/incron/allow

 

 


在 incron 行 git

 

incrontab -l

/home/vhosts/datahunter.org/realtime.txt IN_CLOSE_WRITE /home/vhosts/datahunter.org/scripts/realtime_git-it.sh

realtime_git-it.sh

#!/bin/bash

_trigger_file=/home/vhosts/datahunter.org/realtime.txt
_log=/var/log/git_log/realtime.log
_git_path=/home/vhosts/datahunter.org/public_html
_email='datahunter@datahunter.org'

# git setting
export HOME="/root"

# locking
_scriptname=$(basename $0)
_pidfile="/var/run/$_scriptname.pid"
exec 200>$_pidfile
flock -xn 200 || { echo -n "another instance is running"; exit 1; }
_pid=$$
echo $_pid
echo $_pid 1>&200

# code
if [ -s $_trigger_file ]; then
        echo "cleanup trigger_file & git"
        echo "$@" >> $_log

        # cleanup
        _msg=`cat $_trigger_file | tr '\n' ' ' | tr -d '\r'`
        echo -n "" > $_trigger_file

        # git
        cd $_git_path
        echo `date` >> $_log
        git add . >> $_log 2>&1
        git commit -m "$_msg" >> $_log 2>&1
        git config --file /root/.gitconfig credential.helper store >> $_log 2>&1
        git push >> $_log 2>&1

        # log file
        echo "=============================================" >> $_log

        # e-mail notification
        echo "git time: `date`" | mail -s'notification - server git' $_email
fi

# clean lock
rm -f $_lock

 


FAQ

 

IN_MODIFY 與 IN_CLOSE_WRITE

Why do inotify events fire more than once (IN_MODIFY)

when writing large files it would fire more than once. That sounded fair

The IN_MODIFY event is emitted on a file content change (e.g. via the write() syscall)

while IN_CLOSE_WRITE occurs on closing the changed file.

It means each change operation causes one IN_MODIFY event

(it may occur many times during manipulations with an open file) whereas IN_CLOSE_WRITE is emitted only once (on closing the file).
 

參考: http://datahunter.org/lock