So you may ask, why go through all the trouble?
Have you ever had to move scripts from one directory to another to find out the script is now broken?
This is due to the hard coding of the location for SQL script in the shell script.
When possible, I would like to separate shell / SQL scripts so they can be run individually and easier for debugging.
Don’t you hate it when copy/paste SQL from shell and require modifications?
Example: SELECT count(*) FROM v\$transaction t, v\$session s WHERE t.addr=s.taddr
When the shell & SQL scripts have the same name, it’s more intuitive, as well as the log file.
Example: open_trans.sh, open_trans.sql, /tmp/open_trans_hawklas.log
You can find example of shell scripts with all comments open_trans.sh
DEMO:
oracle@arrow:hawklas:/media/sf_working/scripts
$ ./open_trans.sh
Not enough variables passed to shell script.
./open_trans.sh: line 7: 1: ---> USAGE: ./open_trans.sh [oracle_sid] [minutes]
oracle@arrow:hawklas:/media/sf_working/scripts
$ ./open_trans.sh hawklas 12
The Oracle base remains unchanged with value /u01/app/oracle
+ rm -fv /tmp/open_trans_hawklas.log
removed `/tmp/open_trans_hawklas.log'
++ sqlplus -SL '/ as sysdba'
+ trans_ct=' 1'
+ '[' 0 '!=' 0 ']'
+ '[' ' 1' -eq 0 ']'
+ sqlplus -L '/ as sysdba'
+ '[' 0 '!=' 0 ']'
+ mail -s 'hawklas Open transactions longer than 12 minutes' dba@911.com
+ echo '*** Review /tmp/open_trans_hawklas.log for results ***'
*** Review /tmp/open_trans_hawklas.log for results ***
+ ls -l /tmp/open_trans_hawklas.log
-rw-r--r--. 1 oracle oinstall 950 Oct 4 11:48 /tmp/open_trans_hawklas.log
+ set +x
oracle@arrow:hawklas:/media/sf_working/scripts
$ cat /tmp/open_trans_hawklas.log
*** Open transactions longer than 12 minutes ***
old 17: AND t.start_date < sysdate-(&_minute/1440)
new 17: AND t.start_date < sysdate-(12/1440)
START_DT USERNAME OSUSER MACHINE PROGRAM STATUS EVENT SID_SERIAL TSTATUS SQLID
--------------------- ------------------ ------------ ---------------- ---------------- -------- ------------------------------ ------------ -------- -------------
04-OCT-2015 09:36:55 MDINH oracle arrow.localdomai sqlplus@arrow.lo INACTIVE SQL*Net message from client 108,219 ACTIVE
*** SQL syntax to kill sessions with open transactions ***
old 4: AND t.start_date < sysdate-(&_minute/1440)
new 4: AND t.start_date < sysdate-(12/1440)
SQL
-------------------------------------------------------------------------------------------------------------------------
ALTER SYSTEM KILL SESSION '108,219' IMMEDIATE;
Run SQL script independently:
oracle@arrow:hawklas:/media/sf_working/scripts
$ sysdba @open_trans.sql 23
SQL*Plus: Release 11.2.0.4.0 Production on Sun Oct 4 11:48:34 2015
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
NAME
----------------
hawklas
*** Open transactions longer than 23 minutes ***
old 17: AND t.start_date < sysdate-(&_minute/1440)
new 17: AND t.start_date < sysdate-(23/1440)
START_DT USERNAME OSUSER MACHINE PROGRAM STATUS EVENT SID_SERIAL TSTATUS SQLID
--------------------- ------------------ ------------ ---------------- ---------------- -------- ------------------------------ ------------ -------- -------------
04-OCT-2015 09:36:55 MDINH oracle arrow.localdomai sqlplus@arrow.lo INACTIVE SQL*Net message from client 108,219 ACTIVE
*** SQL syntax to kill sessions with open transactions ***
old 4: AND t.start_date < sysdate-(&_minute/1440)
new 4: AND t.start_date < sysdate-(23/1440)
SQL
-------------------------------------------------------------------------------------------------------------------------
ALTER SYSTEM KILL SESSION '108,219' IMMEDIATE;
"-------------------------------------------------"
+++++ Spool Output: /tmp/open_trans_hawklas.log
"-------------------------------------------------"
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Run shell script from another directory specifying full path:
oracle@arrow:hawklas:/media/sf_working/scripts
$ cd /tmp/
oracle@arrow:hawklas:/tmp
$ /media/sf_working/scripts/open_trans.sh hawlas 45
ORACLE_HOME = [/home/oracle] ? ^C
oracle@arrow:hawklas:/tmp
$ /media/sf_working/scripts/open_trans.sh hawklas 45
The Oracle base remains unchanged with value /u01/app/oracle
+ rm -fv /tmp/open_trans_hawklas.log
removed `/tmp/open_trans_hawklas.log'
++ sqlplus -SL '/ as sysdba'
+ trans_ct=' 1'
+ '[' 0 '!=' 0 ']'
+ '[' ' 1' -eq 0 ']'
+ sqlplus -L '/ as sysdba'
+ '[' 0 '!=' 0 ']'
+ mail -s 'hawklas Open transactions longer than 45 minutes' dba@911.com
+ echo '*** Review /tmp/open_trans_hawklas.log for results ***'
*** Review /tmp/open_trans_hawklas.log for results ***
+ ls -l /tmp/open_trans_hawklas.log
-rw-r--r--. 1 oracle oinstall 950 Oct 4 11:49 /tmp/open_trans_hawklas.log
+ set +x
oracle@arrow:hawklas:/tmp
$ cat /tmp/open_trans_hawklas.log
*** Open transactions longer than 45 minutes ***
old 17: AND t.start_date < sysdate-(&_minute/1440)
new 17: AND t.start_date < sysdate-(45/1440)
START_DT USERNAME OSUSER MACHINE PROGRAM STATUS EVENT SID_SERIAL TSTATUS SQLID
--------------------- ------------------ ------------ ---------------- ---------------- -------- ------------------------------ ------------ -------- -------------
04-OCT-2015 09:36:55 MDINH oracle arrow.localdomai sqlplus@arrow.lo INACTIVE SQL*Net message from client 108,219 ACTIVE
*** SQL syntax to kill sessions with open transactions ***
old 4: AND t.start_date < sysdate-(&_minute/1440)
new 4: AND t.start_date < sysdate-(45/1440)
SQL
-------------------------------------------------------------------------------------------------------------------------
ALTER SYSTEM KILL SESSION '108,219' IMMEDIATE;
oracle@arrow:hawklas:/tmp
$
oracle@arrow:hawklas:/tmp
$ /media/sf_working/scripts/open_trans.sh hawklas 44444445 > /tmp/cron.log 2>&1
oracle@arrow:hawklas:/tmp
$ cat /tmp/cron.log
The Oracle base remains unchanged with value /u01/app/oracle + rm -fv /tmp/open_trans_hawklas.log removed `/tmp/open_trans_hawklas.log' ++ sqlplus -SL '/ as sysdba' + trans_ct=' 0' + '[' 0 '!=' 0 ']' + '[' ' 0' -eq 0 ']' + echo 'Number of opened transactions: 0' Number of opened transactions: 0 + exit 1 oracle@arrow:hawklas:/tmp $ ls open* ls: cannot access open*: No such file or directory oracle@arrow:hawklas:/tmp $
