1. 編寫sql指令碼
編寫DB2需要執行的sql執行指令碼,並存放到AIX伺服器上。
樣本如下crontest.sql:
- connect to tem user db2admin using db2admin;
- call P_TEST();
2. 編寫sh執行檔案
編寫sh檔案,並存放到AIX伺服器上,並修改sh檔案為可執。
樣本如下crontest.sh:
- PATH=/usr/bin:/etc:/usr/sbin:/usr/ucb:$HOME/bin:/usr/bin/X11:/sbin:.
- export PATH
- if [ -s "$MAIL" ] # This is at Shell startup. In normal
- then echo "$MAILMSG" # operation, the Shell checks
- fi # periodically.
- # The following three lines have been added by UDB DB2.
- if [ -f /home/db2admin/das/dasprofile ]; then
- ./home/db2admin/das/dasprofile
- fi
- # The following three lines have been added by UDB DB2.
- if [ -f /home/db2admin/sqllib/db2profile ]; then
- ./home/db2admin/sqllib/db2profile
- fi
-
- #execute sql script
- db2 -svtf /home/db2admin/task/crontest.sql -z /home/db2admin/task/crontest.log
註:sh檔案的第一部分為設定db2執行需要的環境變數(必須設定),可以根據具體的實際情況進行修改,或直接將db2admin的.profile檔案中的變數直接複製過來。
- #修改sh檔案的屬性
- chmod 777 crontest.sh
3. 配置crontab
用db2admin使用者登陸AIX系統,使用crontab –e命令進行定時任務設定。crontab的具體命令參見附註中的說明。
樣本如下:
- # 設定每天02:00分定時執行crontest.sh指令碼
- 00 02 * * * /home/db2admin/task/crontest.sh
存檔退出即可。