This article mainly tells you the example of using crontab to regularly execute DB2 SQL. Today, we tested it in RHEL 5.3. Common users use crontab to periodically execute DB2 SQL operations, it took some detours because of the usage. /The execution result is correct.
While crontab cannot be executed, it is very depressing. I checked it online for a long time, and many said it was a permission issue for/usr/bin/crontab.
But I can check it at RHEL 5.3.
- -rwsr-sr-x 1 root root 309K 2007-06-22 /usr/bin/crontab
My permissions are correct. I checked a lot of information to find out that it was a problem with the DB2 environment variables, mainly because the level is not enough and the consciousness is not high. I wrote down the backup
Log on to linux as a db2inst1 user
- 1. vi db2inst1cron
- * ***/Home/db2inst1/tongji. sh>/home/db2inst1/B .txt
- 2. crontab db2inst1cron
- 3. vi tongji. sh (1, 777)
- #! /Bin/bash
- # Tongji auto
- Top = "20"
- Qiantian = $ top 'date-d' 2 day ago '+ "% y % m % d "'
- Yesterday = $ top 'date-d' 1 day ago '+ "% y % m % d "'
- Echo "----------------------------------------------"
- Echo $ (date + "% y-% m-% d % H: % M: % S ")
- Echo "----------------- setp1: date --------------------"
- Echo "qiantian:" $ qiantian
- Echo "yesterday:" $ yesterday
- Echo "----------------- setp2: char ---------------------"
- # Sed-I "s/$ qiantian/$ yesterday/g" 'grep $ qiantian-rl/home/db2inst1/tongji_sec'
- Echo "---------------- step3: dbconnect -----------------"
- Db2 "connect to toolsdb"
- Echo & quot; 1232312312 & quot"
- Db2-tf/home/db2inst1/tongji_sec/SQL _sec. SQL
- 4. vi SQL _sec. SQL
- Insert into liuchao values (10 );
- A.
- [Db2inst1 @ localhost ~] $./Tongji. sh is correct and the result is desired)
- 10-03-09 15:37:27
- ----------------- Setp1: date --------------------
- Qiantian: 20100307
- Yesterday: 20100308
- ----------------- Setp2: char ---------------------
- ---------------- Step3: dbconnect -----------------
- Database Connection Information
- Database server = DB2/LINUXX8664 9.5.3
- SQL authorization ID = DB2INST1
- Local database alias = TOOLSDB
- 1232312312
- DB20000I The SQL command completed successfully.
- [Db2inst1 @ localhost ~] $
-
- B. crontab execution error, no database operation, not expected result)
- 10-03-09 15:38:01
- ----------------- Setp1: date --------------------
- Qiantian: 20100307
- Yesterday: 20100308
- ----------------- Setp2: char ---------------------
- ---------------- Step3: dbconnect -----------------
- 1232312312
Solution
- #! /Bin/bash
- # Tongji auto
- ./Home/db2inst1/sqllib/db2profile # introduce the db2 environment variable. There is a space behind it)
- Top = "20"
The above content is an introduction to regular users who use crontab to regularly execute DB2 SQL examples. I hope you will gain some benefits.