Linux上定时shell脚本

原文链接: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保存并退出

posted @ 2019-12-17 10:22  章朔  阅读(2986)  评论(0编辑  收藏  举报