版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/anitinaj/article/details/80901404
查看定时任务
crontab -l
- 注册定时任务
crontab -e
然后就像 vim 一样编辑自己的定时任务。
如:
* * * * * . /home/hadoop/timer/check_job.sh >> /home/hadoop/timer/logs/check_job.log
- 定时任务解析
- *和*之间的空格,是必须的
- *分别代表:分,时,日,月,周
1. 分 的取值范围为:0-59
2. 时 的取值范围为:0-23
3. 日 的取值范围为:1-31
4. 月 的取值范围为:1-12
5. 周 的取值范围为:0-7
- *后面是需要执行的命令
- "." 代表是shell 命令,也可以使用 sh
- ">>" 前面的是需要定时调用的shell,后面是 需要输出的 log 文件的位置
- 实例
1. * * * * * 代表每分钟执行一次,也可以写成 */1 * * * *,/1代表一分钟执行一次
2. 30 */1 * * * 代表每小时的 30 分执行一次
以下是对文件的操作
- 根据文件名称取得最大日期对文件
- 文件名称的格式为:filter_cache_20171101.se
max_date=0 # get the latest file and copy to hdfs cd /home/hadoop/.../checkpoint/filter for dir in $(ls -l ./ | awk '/^d/{print $NF}') do if [[ -d $dir && $dir == *\_* ]]; then f_d=$(echo $dir | cut -d \_ -f 3 | cut -d \. -f 1) if [[ $max_date < $f_d ]]; then max_date=$f_d max_filter=$dir fi fi done echo " max date is : "$max_date echo " max filter is : "$max_filter
- 复制最近日期对文件到hdfs
- 如果文件存在,提示已经存在
hadoop fs -test -e /data/.../bloom_filters/$max_filter if [[ $? == 0 ]]; then echo " filter is already exist : "$max_filterF else echo " start hdfs copy " hadoop fs -put $max_filter /data/.../bloom_filters fi
- 设定需要删除过期文件的指定时间
remove_week=$(date -d "$max_date 7 days ago" +%Y%m%d) echo " 删除本地序列化文件的日期界限:"$remove_week remove_date=$(date -d "$max_date 30 days ago" +%Y%m%d) echo " 删除文件 和 Hadoop filter 的日期界限:"$remove_date
- 删除指定日期30天以外的本地文件,并且删除小于指定日期7天以外的指定文件夹的文件
for r_dir in $(ls -l ./ | awk '/^d/{print $NF}') do if [[ -d $r_dir && $r_dir == *\_* ]]; then r_d=$(echo $r_dir | cut -d \_ -f 3 | cut -d \. -f 1) if [[ $r_d < $remove_date ]]; then echo " remove filter_dir is: "$r_dir rm -r $r_dir elif [[ $r_d < $remove_week ]]; then echo " remove filter is: "$r_dir rm -r $r_dir/*.FILTER.SER fi fi done
- 删除hdfs上指定日期30以外的文件
for h_filter in $(hadoop fs -ls /data/.../bloom_filters | awk '{print $8}') do if [[ $h_filter == *\_* ]]; then h_date=$(echo $h_filter | cut -d \/ -f 6 | cut -d \_ -f 3 | cut -d \. -f 1) # echo " hdfs date : "$h_date # echo " hdfs filter : "$h_filter if [[ $h_date < $remove_date ]]; then echo " remove hdfs filter is : "$h_filter hadoop fs -rmr $h_filter fi fi done