Objective: run db2 script at the specified time automatically and send log file out.
Environment: RHEL5 with db2 V9.0
Script:
- write script as below
#!/bin/sh
export PATH=/db2/db2inst1/sqllib/bin:/usr/bin:/bin
. /db2/db2inst1/sqllib/db2profile
cd /db2/db2inst1/scripts/20091210
FILE=open.sql
db2 connect to db1
db2 set schema = schema1
db2 -tvf $FILE -l ${FILE}.log
db2 terminate
sleep 5
scp ${FILE}.log backup@mon1:/tmp
sleep 5
#copy log file to email server and run email command there
ssh -t backup@mon1 "email -b -s "script result" jephe@domain.com < /tmp/${FILE}.log"
2. make it run as cronjob
#chmod +x /db2/db2inst1/scripts/20091210/open.sh
then crontab -e to add the following to run it at 9am 5 Jan
0 9 5 1 * /db2/db2inst1/scripts/20091210/open.sh > /dev/null 2>&1
note:
1. make sure all your command used in the script are in /usr/bin or /bin, otherwise, put the necessary paths inside the script export line.
2. put open.sql under /db2/db2inst1/scripts/20091210/
3. some tips - open and close code
update something set code=concat(code,'_DISABLE') where something;
update something set code=replace(code,'_DISABLE','') where something;