原文链接:http://www.92coder.com/9-Linux%E5%AE%9A%E6%97%B6shell%E8%84%9A%E6%9C%AC/#more
本文主要介绍在Linux系统上部署定时器,定时执行shell脚本,通过脚本执行sql文件
sql文件
1-- 创建表 2create table if not exists iot_test.iot_tac 3( 4MSISDN string, 5TAC string 6) 7partitioned by(day string) 8row format delimited 9fields terminated by '\t' 10lines terminated by '\n' 11stored as parquet; 12 13--录入数据 14set hive.exec.dynamic.partition.mode=nonstrict; 15insert overwrite table iot_test.iot_tac 16partition(day='${hivevar:day}') 17select t4.MSISDN,t4.TAC 18from 19(select t1.MSISDN,t1.TAC from 20(select MSISDN,substr(IMEI,1,8) as TAC,row_number()over(partition by MSISDN) as rn 21from prestat.iot_activeuser_hour 22where day='${hivevar:day}' and minute='${hivevar:minute}' and IMEI is not null) as t1 23left join 24(select MSISDN,TAC from iot_test.iot_tac where day='${hivevar:lmonth}') as t2 25on t1.MSISDN = t2.MSISDN 26where t2.MSISDN is null and t1.rn = 1 27union all 28select MSISDN,TAC 29from iot_test.iot_tac 30where day='${hivevar:lmonth}' 31) as t4;
- ${hivevar:day} 接受shell脚本传来的参数day
shell脚本
1#!/bin/bash 2source /etc/profile 3set -e 4echo "**************************************************" 5echo "**************************************************" 6echo "*********************START************************" 7echo "**************************************************" 8echo "**************************************************" 9day=$(date -d "today -5hours" +%Y%m%d) 10minute=$(date -d "today -5hours" +%H00) 11lmonth=$(date -d "last month -5hours" +%Y%m%d) 12echo $day 13echo $minute 14echo $lmonth 15 16kinit -kt /home/secu01/cluster_keytab/secu01.keytab secu01 17 18#调用sql 19/usr/bin/hive -hivevar cmouth=${day} -hivevar cmouth=${minute} -hivevar lmouth=${lmonth} -f /iot_tac.sql 20 21echo "*************iot_tac.sql调用成功*************" 22echo "***************all success****************"
#!/bin/bash指此脚本使用/bin/bash来解释执行- day、minute、lmonth 定义的参数,传递给sql文件
部署定时
-
第一步:将shell脚本和sql文件上传到Linux系统中,shell文件名:iot_tac.sh;sql文件名:iot_tac.sql
-
第二步:更改shell脚本的权限
chmod u+x iot_tac.sh
-
第三步:如果sql文件报错:/bin/bash^M: bad interpreter
sed -i "s/\r//" iot_tac.sql
-
第四步:设置定时器 (1)增加一个cron定时任务
crontab -e
(2)按insert键进入编辑模式
00 * * * * /home/zhangs/iot_tac.sh >/home/zhangs/log/iot_tac.log
表示每小时执行一次shell脚本,并生成日志文件 minute: 区间为 0 – 59 hour: 区间为0 – 23 day-of-month: 区间为0 – 31 month: 区间为1 – 12. 1 是1月. 12是12月. Day-of-week: 区间为0 – 7. 周日可以是0或7. (3)按esc键退出编辑模式,再按shift+:输入:wq保存并退出