1.编写存储过程
--设置指向的数据库
SET SCHEMA = DB2INST1;
--设置当前的路径
SET CURRENT PATH =
"SYSIBM","SYSFUN","SYSPROC","SYSIBMADM","DB2INST1";
--创建存储过程
CREATE PROCEDURE "DB2INST1".monitor_lingjian()
Begin
--删除本地化零件(009)相关的数据
delete from cs_coc_deal_infos where SYS_ID='009';
--插入统计的本地化零件相关的数据至cs_coc_deal_infos表中
insert into cs_coc_deal_infos(SYS_ID,DEAL_NUM,DEAL_USER)
select '009' as SYS_ID,sum(num) as DEAL_NUM,DESINGER from(
select count(*) as num,DESINGER from SOA_LPP_TM_TASK_V group by DESINGER
Union select count(*) as num,DESINGER from SOA_LPP_LP_TASK_V group by DESINGER
Union select count(*) as num,DESINGER from SOA_LPP_LAR_TASK_V group by DESINGER
Union select count(*) as num,DESINGER from SOA_LPP_PPAPAPP_TASK_V group by DESINGER
Union select count(*) as num,DESINGER from SOA_LPP_TAR_TASK_V group by DESINGER
Union select count(*) as num,DESINGER from SOA_LPP_TAN_TASK_V group by DESINGER
Union Select count(*) as num,DESINGER from SOA_LPP_KD_TASK_V group by DESINGER
Union Select count(*) as num,DESINGER from SOA_LPP_CSL_TASK_V group by DESINGER) group by DESINGER;
--插入累加的总待办数量给wpsadmin用户
insert into cs_coc_deal_infos(SYS_ID,DEAL_NUM,DEAL_USER)
select '009' as SYS_ID,sum(deal_num) as deal_num,'wpsadmin' as deal_user from cs_coc_deal_infos where SYS_ID='009' and DEAL_USER<>'wpsadmin';
end
--测试存储过程是否创建成功
call "DB2INST1".monitor_lingjian()
2.创建shell脚本(monitor_lingjian.sh)
#! /bin/sh
[注释:注意#打头什么的都去掉吧,这是干扰crontab执行脚本的重要原因!]
DB2_HOME=/opt/ibm/db2/V9.7
PATH=$DB2_HOME/bin:$PATH
export $DB2_HOME
export $PATH
[注释:必须要设置导出一下相关命令(DB2)的环境变量,否则crontab无法正常识别执行]
Tips:其实这个可以写在etc文件夹中的profile文件中的,然后导入profile文件,但是众所周知,profile文件中配置整个系统中一系列的环境变量的,如果配置在这个文件中那么所有的用户都可以使用到该环境变量,从安全性这方面考虑呢,还是最好就将需要使用的命令的环境变量配置在脚本中即可。 |
startTime=`date +%Y%m%d%H%M%S`
echo $startTime
echo "开始执行脚本"
db2 connect to STSUPM2 user db2inst1 using db2inst1
[注释:注意不仅要连接到数据库还要连接到存储过程所在实例名]
echo "连接上数据库,开始执行脚本"
db2 "call monitor_lingjian()"
endTime=`date +%Y%m%d%H%M%S`
echo $endTime
echo "脚本执行完毕!关闭数据库连接"
db2 terminate
附:脚本中包含一些时间或者文字记录,有利于跟踪脚本的执行
3.使用crontab创建定时任务
1) 创建定时任务
在etc文件夹下面找到crondtab文件,在其最后一行加上
*/10 * * * * root /opt/monitor_lingjian.sh>>/opt/monitor_lingjian.log
格式注释:分钟 小时 日 月 星期 用户 脚本绝对路径>>日志文件所在绝对路径
表示每10分钟执行一次脚本文件monitor_lingjian.sh,并将打印信息输出在
monitor_lingjian.log文件中
2) 重启crond服务
[root@localhost ~]# /etc/init.d/crond restart
如果让crond在开机时运行,应该改变其运行级别
[root@localhost ~]# chkconfig --levels 35 crond on