原文鏈接:http://www.92coder.com/9-Linux定時shell腳本/#more
本文主要介紹在Linux系統上部署定時器,定時執行shell腳本,通過腳本執行sql文件
sql文件
-- 創建表
create table if not exists iot_test.iot_tac
(
MSISDN string,
TAC string
)
partitioned by(day string)
row format delimited
fields terminated by '\t'
lines terminated by '\n'
stored as parquet;
--錄入數據
set hive.exec.dynamic.partition.mode=nonstrict;
insert overwrite table iot_test.iot_tac
partition(day='${hivevar:day}')
select t4.MSISDN,t4.TAC
from
(select t1.MSISDN,t1.TAC from
(select MSISDN,substr(IMEI,1,8) as TAC,row_number()over(partition by MSISDN) as rn
from prestat.iot_activeuser_hour
where day='${hivevar:day}' and minute='${hivevar:minute}' and IMEI is not null) as t1
left join
(select MSISDN,TAC from iot_test.iot_tac where day='${hivevar:lmonth}') as t2
on t1.MSISDN = t2.MSISDN
where t2.MSISDN is null and t1.rn = 1
union all
select MSISDN,TAC
from iot_test.iot_tac
where day='${hivevar:lmonth}'
) as t4;
- ${hivevar:day}
接受shell腳本傳來的參數day
shell腳本
#!/bin/bash
source /etc/profile
set -e
echo "**************************************************"
echo "**************************************************"
echo "*********************START************************"
echo "**************************************************"
echo "**************************************************"
day=$(date -d "today -5hours" +%Y%m%d)
minute=$(date -d "today -5hours" +%H00)
lmonth=$(date -d "last month -5hours" +%Y%m%d)
echo $day
echo $minute
echo $lmonth
kinit -kt /home/secu01/cluster_keytab/secu01.keytab secu01
#調用sql
/usr/bin/hive -hivevar cmouth=${day} -hivevar cmouth=${minute} -hivevar lmouth=${lmonth} -f /iot_tac.sql
echo "*************iot_tac.sql調用成功*************"
echo "***************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保存並退出