文档说明
该文档是介绍Linux中时间触发命令的使用,使用场景是当需要有触发事件执行命令的需求,比如如果某个文本被修改了,立即删除另外一个文本。
系统环境
操作系统:Linux version 3.10.0-229.el7.x86_64 ([email protected]) (gcc version 4.8.2 20140120 (Red Hat 4.8.2-16) (GCC) ) #1 SMP Fri Mar 6 11:36:42 UTC 2015
操作步骤
一般系统没有默认安装incrontab,所以需要自行下载安装。
1.下载incrontab rpm包
下载地址为:http://pkgs.repoforge.org/incron/
需要选择的incrontab rpm安装包版本号为:incron-0.5.9-2.el6.rf.x86_64.rpm
注:由于操作系统环境是centos7,故incron版本必须在0.5.9以上,否则无法运行
2.安装rpm包
安装rpm包将rpm包下载到指定文件夹里(本文指定目录为/soft/installation), 并且安装rpm包
rpm -ivh incron-0.5.9-2.el6.rf.x86_64.rpm
注:其中rpm 所带参数 -ivh 代表安装rpm包并显示进度情况(--install--verbose--hash)
3.开启incrontab 服务
/etc/init.d/incrond start
4.添加触发任务
本例题中所添加的任务是,如果/soft/temp/test_incrontab.txt 文本中的文件被修改,即运行脚本 /soft/bin/startupall.sh
首先需要配置incrontab任务表格
incrontab -e
注:e代表edit,修改配置
编辑incrontab表格,加入如下语句
/soft/temp/test_incrontab.txt IN_MODIFY /soft/bin/startupall.sh
注:tomcat自带的/bin/startup.sh 无法在incrontab中运行,原因是没有加上 export JAVA_HOME=/soft/jdk8。
: wq 保存
注:语句的格式是: <文件或文件夹路径> <事件名> <操作>
<file path> <event> <action>
具体的事件名见最后一页
5.显示incrontab表格确认任务
incrontab -l
6.查看日志
当修改test_incrontab.txt 内容后,可以查看日志,看看incrontab 是否有监测到文件变化并执行。
tail -f /var/log/cron
如图所示,表示脚本startupall.sh已执行。
附录:
事件名
Event |
Meaning |
Common Events |
|
IN_ACCESS |
File was accessed (read) |
IN_ATTRIB |
Metadata was changed (permissions, timestamps, extended attributes, etc.) |
IN_CLOSE_WRITE |
File opened for writing was closed |
IN_CLOSE_NOWRITE |
File not opened for writing was closed |
IN_CLOSE |
Combines IN_CLOSE_WRITE and IN_CLOSE_NOWRITE |
IN_CREATE |
File/directory created in watched directory |
IN_DELETE |
File/directory deleted from watched directory |
IN_DELETE_SELF |
Watched file/directory was deleted |
IN_MODIFY |
File was modified |
IN_MOVE_SELF |
Watched file/directory was moved |
IN_MOVED_FROM |
File moved out of watched directory |
IN_MOVED_TO |
File moved into watched directory |
IN_MOVE |
A combination of IN_MOVED_FROM and IN_MOVED_TO. |
IN_OPEN |
File was opened |
Special Events |
|
IN_ALL_EVENTS |
Combines all of the above events |
IN_DONT_FOLLOW |
Don‘t dereference pathname if it is a symbolic link |
IN_ONESHOT |
Monitor pathname for only one event |
IN_ONLYDIR |
Only watch pathname if it is a directory |
Wildcard Event |
|
IN_NO_LOOP |
Disable monitoring of events until the current event is handled completely (until its child process exits – avoids infinite loops) |
------------------完------------------