13
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

sqlplusでSQLを定期実行

Last updated at Posted at 2014-04-22

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

sesw.sh
#!/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

13
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?