离线的操作系统iso
中没有这个包,这里使用阿里的yum
原下载安装
# 备份本地源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo_bak
# 配置阿里的
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all
yum makecache
如果阿里域名无法解析,配置以下文件
cat >> /etc/resolv.conf <<EOF
nameserver 8.8.8.8
nameserver 8.8.4.4
EOF
开始安装
# 先安装扩展包源,否则inotify-tools找不到
yum install epel-release
yum install inotify-tools
[root@manager srcdir]# ls -l /proc/sys/fs/inotify
total 0
-rw-r--r-- 1 root root 0 Aug 5 21:28 max_queued_events
-rw-r--r-- 1 root root 0 Aug 5 21:28 max_user_instances
-rw-r--r-- 1 root root 0 Aug 5 21:28 max_user_watches
在proc/sys/fs/inotify目录下的三个文件,对inotify机制有一定的限制
max_user_watches:设置inotifywait或inotifywatch命令可以监视的文件数量(单进程)
max_user_instances:设置每个用户可以运行的inotifywait或inotifywatch命令的进程数。
max_queued_events:设置inotify实例事件(event)队列可容纳的事件数量。
编写脚本demo1.sh
[root@manager inotifytest]# ll
total 4
-rwxr-xr-x 1 root root 184 Aug 5 21:45 demo1.sh
drwxr-xr-x 2 root root 6 Aug 5 21:43 srcdir
#!/bin/bash
srcdir=/opt/inotifytest/srcdir
inotifywait -rq --timefmt '%d/%m/%y-%H:%M' --format '%T %w%f' -e modify,create,attrib ${srcdir} \
| while read file
do
echo "${file} is notified!"
done
-m 持续监听
-r 使用递归形式监视目录
-q 减少冗余信息,只打印出需要的信息
-e 指定要监视的事件,多个时间使用逗号隔开
–timefmt 时间格式
–format 监听到的文件变化的信息ymd分别表示年月日,H表示小时,M表示分钟
–format 说明:
%w 表示发生事件的目录
%f 表示发生事件的文件
%e 表示发生的事件
%Xe 事件以“X”分隔
%T 使用由–timefmt定义的时间格式
保存退出,给脚本增加可执行权限
[root@manager inotifytest]# chmod +x demo1.sh
[root@manager inotifytest]# ll
total 4
-rwxr-xr-x 1 root root 184 Aug 5 21:45 demo1.sh
drwxr-xr-x 2 root root 6 Aug 5 21:43 srcdir
运行demo1.sh
这后,在srcdir
目录中任意创建一个文件,就输入程序中定义的信息
[root@manager inotifytest]# sh demo1.sh
05/08/20-22:27 /opt/inotifytest/srcdir/3.dat is notified!
脚本内容:
#!/bin/bash
srcdir=/opt/inotifytest/srcdir
inotifywait -mrq --timefmt '%d/%m/%y-%H:%M' --format '%T %w%f' -e create ${srcdir} \
| while read file
do
echo "${file} is notified!"
NEWFILE=`echo ${file} | cut -d ' ' -f 2`
echo "${NEWFILE}"
done
在–format参数中%T后面有一个空格,所以时间和文件名就以空格拆分
05/08/20-22:51 /opt/inotifytest/srcdir/15.dat is notified!
/opt/inotifytest/srcdir/15.dat
以demo3.sh
为例
修改/etc/rc.d/rc.local
文件
先将rc.local
增加可执行权限
[root@manager rc.d]# chmod +x rc.local
修改内容
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
nohup /opt/inotifytest/demo3.sh >> /var/log/inotify_flume.log &
重启以验证开机启动效果
安装定时任务服务
[root@manager opt]# yum -y install crontabs
查看定时任务服务状态
[root@manager centos7]# systemctl status crond
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2020-08-09 15:10:01 CST; 26min ago
Main PID: 530 (crond)
CGroup: /system.slice/crond.service
└─530 /usr/sbin/crond -n
Aug 09 15:10:01 manager.learn systemd[1]: Started Command Scheduler.
Aug 09 15:10:01 manager.learn systemd[1]: Starting Command Scheduler...
Aug 09 15:10:01 manager.learn crond[530]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 45% if used.)
Aug 09 15:10:01 manager.learn crond[530]: (CRON) INFO (running with inotify support)
修改/etc/crontab
文件增加定时任务
[root@manager cronbak]# vim /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
# 每隔1分钟执行下边的命令,测试
*/1 * * * * root /opt/inotifytest/cron_bak.sh
查看日志
[root@manager log]# tail -100f /var/log/cron
Aug 9 16:02:01 manager CROND[1337]: (root) CMD ( /opt/inotifytest/cron_bak.sh)
Aug 9 16:03:01 manager CROND[1352]: (root) CMD ( /opt/inotifytest/cron_bak.sh)
Aug 9 16:04:01 manager CROND[1378]: (root) CMD ( /opt/inotifytest/cron_bak.sh)
Aug 9 16:05:01 manager CROND[1398]: (root) CMD ( /opt/inotifytest/cron_bak.sh)
Aug 9 16:06:02 manager CROND[1406]: (root) CMD ( /opt/inotifytest/cron_bak.sh)
Aug 9 16:07:01 manager CROND[1418]: (root) CMD ( /opt/inotifytest/cron_bak.sh)
Aug 9 16:08:01 manager CROND[1433]: (root) CMD ( /opt/inotifytest/cron_bak.sh)
Aug 9 16:09:01 manager CROND[1443]: (root) CMD ( /opt/inotifytest/cron_bak.sh)
Aug 9 16:10:01 manager CROND[1448]: (root) CMD ( /opt/inotifytest/cron_bak.sh)
脚本内容
#!/bin/bash
srcdir="/data/meteorological_data"
destinatedir="/data/meteorological_data_bak"
flumedatadir="/flumedata/meteorological_data"
# nowdate=$(date "+%Y-%m-%d %H:%M:%S")
nowdate=$(date -d "-1 days" +"%Y-%m-%d")
# nowdate=$(date -d "-1 days 2020-08-07" +"%Y-%m-%d")
# startdate="$nowdate 00:00:00"
enddate="$nowdate 23:59:59"
echo "$nowdate"
echo "$startdate"
echo "$enddate"
for dir in `ls $srcdir`
do
subsrcdir="$srcdir/$dir"
subdesdir="$destinatedir/$dir"
if [ -d "$subsrcdir" ];then
# check destinate dictionary
[ ! -d "$subdesdir" ] && mkdir $subdesdir
# bake file here
daydocument="$subdesdir/${dir}_$nowdate"
[ ! -d "$daydocument" ] && mkdir $daydocument
# bake data here
echo "begin to move data file to $daydocument which create time lower than $enddate"
find $subsrcdir -maxdepth 2 -type f -name '*.dat' -not -newermt "$enddate" -exec mv {} $daydocument \;
echo "move successfully for date $nowdate"
else
echo "$srcdir/${dir} is not a dectionary so skipped..."
fi
done
for file in `ls $flumedatadir`
do
echo "begin to delete *.COMPLETED which create time lower than $enddate"
find $flumedatadir -maxdepth 2 -type f -name '*.COMPLETED' -not -newermt "$enddate" -exec rm {} \;
echo "delete successfully for date $nowdate"
done