文档说明
该文档是介绍Linux中时间触发命令的使用,使用场景是当需要有触发事件执行命令的需求,比如如果某个文本被修改了,立即删除另外一个文本。
系统环境
操作系统:Linux version 3.10.0-229.el7.x86_64 (builder@kbuilder.dev.centos.org) (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包
需要选择的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 保存
注:语句的格式是:
具体的事件名见最后一页
5.显示incrontab表格确认任务
incrontab -l
6.查看日志
当修改test_incrontab.txt 内容后,可以查看日志,看看incrontab是否有监测到文件变化并执行。
tail -f /var/log/cron
如图所示,表示脚本startupall.sh已执行。
附录:
事件名EventMeaning
Common Events
IN_ACCESSFile was accessed (read)
IN_ATTRIBMetadata was changed (permissions, timestamps, extended attributes, etc.)
IN_CLOSE_WRITEFile opened for writing was closed
IN_CLOSE_NOWRITEFile not opened for writing was closed
IN_CLOSECombinesIN_CLOSE_WRITEandIN_CLOSE_NOWRITE
IN_CREATEFile/directory created in watched directory
IN_DELETEFile/directory deleted from watched directory
IN_DELETE_SELFWatched file/directory was deleted
IN_MODIFYFile was modified
IN_MOVE_SELFWatched file/directory was moved
IN_MOVED_FROMFile moved out of watched directory
IN_MOVED_TOFile moved into watched directory
IN_MOVEA combination ofIN_MOVED_FROMandIN_MOVED_TO.
IN_OPENFile was opened
Special Events
IN_ALL_EVENTSCombines all of the above events
IN_DONT_FOLLOWDon't dereference pathname if it is a symbolic link
IN_ONESHOTMonitor pathname for only one event
IN_ONLYDIROnly watch pathname if it is a directory
Wildcard Event
IN_NO_LOOPDisable monitoring of events until the current event is handled completely (until its child process exits – avoids infinite loops)
------------------完------------------