Skip to content

Instantly share code, notes, and snippets.

@yasushiyy
Created July 2, 2014 04:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yasushiyy/99d4cb79735b695e50ee to your computer and use it in GitHub Desktop.
Save yasushiyy/99d4cb79735b695e50ee to your computer and use it in GitHub Desktop.
SQL*PlusでSQLの定期実行

1秒単位とかで、定期的にSQLを実行したいときのやり方。

#!/bin/sh

cd `dirname $0`

LOCKFILE=.lock_sesw_`date "+%m%d%H%M%S"`
rm -f .lock_sesw_*
touch $LOCKFILE
trap "echo terminated; rm -f $LOCKFILE; exit 0" INT

sesw()
{
  echo "connect / as sysdba"
  echo "set pages 9999"
  echo "set lines 200"
  echo "set feedback off"
  while [ -f $LOCKFILE ]
  do
    cat << EOF
col TIME      for a10
col SID       for 9999
col MACHINE   for a15 trunc
col PROGRAM   for a15 trunc
col MODULE    for a15 trunc
col SQL_ID    for a15
col COMMAND   for 999
col EVENT     for a30 trunc
col SIW       for 999

SELECT to_char(sysdate, 'mmddhh24miss') TIME
     , sid
     , machine
     , program
     , module
     , sql_id
     , command
     , event
     , seconds_in_wait SIW
  FROM v\$session
 WHERE wait_class != 'Idle'
 ORDER BY event, p1;
EOF
    sleep 1
  done
}

sesw | sqlplus -s /nolog

上の例だと、DBインスタンスのアクティブセッション情報を定期的に出力しますが、どのSQLに置き換えてもよいかと思います。v$ビューの場合、「$」のエスケープだけは忘れずに。

このあたりにも幾つか上げています。 https://github.com/yasushiyy/orashell/blob/master/sesw.sh https://github.com/yasushiyy/orashell/blob/master/seswsmall.sh https://github.com/yasushiyy/orashell/blob/master/sesw9i.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment