Oracle/Admin2010. 10. 13. 22:49
2010. 10. 15일 dbms_scheduler를 이용한 스케쥴링 추가


스케쥴링 하려는 시나리오는 다음과 같다.
매주 월요일 10:00에 전주 월요일부터 일요일까지의 데이터를 통계를 낸다.
실행되는 스크립트는
get_weekly_stats.sh이며, 이 스크립트가 통계를 내는 것인데
sqlplus에 접속하여 html의 형식으로 result_stats_20101013.html과 같은 이름의 파일이 생성되면 된다.



# 통계의 소스가 되는 테이블은 아래와 같습니다.

create table t1 (col1 number, col2 date);

insert into t1
select level, sysdate - 7
from dual
connect by level  <= 10
union
select level*2, sysdate -6
from dual
connect by level <= 10
union
select level*3, sysdate -5
from dual
connect by level <= 10
union
select level*4, sysdate -4
from dual
connect by level <= 10
union
select level*5, sysdate -3
from dual
connect by level <=10
union
select level*6, sysdate -2
from dual
connect by level <= 10
union
select level*7, sysdate -1
from dual
connect by level <= 10;

commit;



# 통계정보를 취득하는 쿼리는 아래와 같습니다.

select sum(col1), col2
from t1
where col2 between next_day(sysdate,'mon')-14
         and next_day(sysdate,'sun')-7
group by col2
order by col2;



통계정보 생성용 스크립트의 내용은 아래와 같고, 주의할 점은 이 스크립트 파일에 대해 oracle 유저가 실행권한을 갖고 있어야 합니다.
get_weekly_stats.sh

sqlplus / as sysdba << EOF

set markup html on
set feedback off
set heading off
set echo off
set pages 1000

column date_column new_value today_var
select to_char(sysdate,'yyyymmdd') date_column
from dual
/

set heading on
spool result_stats_&today_var..html

select sum(col1), col2
from t1
where col2 between next_day(sysdate,'mon')-14
      and next_day(sysdate,'sun')-7
group by col2
order by col2
/

spool off

EOF



os 명령어 중 crontab을 이용하면 손쉽게 스케쥴링을 할 수 있다.
아래의 내용을 참고해서 매주 월요일 10:00에 get_weekly_stats.sh을 실행시키도록 crontab에 설정해보면 ...


os] crontab -e    => crontab 편집
00 10 * * 1 /home/oracle/get_weekly_stats.sh
os] crontab -l     => 설정된 내용 확인


위와 같이 설정을 하면 매주 월요일 10:00에 get_weekly_stats.sh 스크립트가 잘 실행됩니다.


########## crontab 참고 내용 ############


crontab에 등록된 작업 보기
os] crontab -l

crontab에 작업 등록 & 수정 하기
os] crontab -e

※작업 등록시 주의할 점
  -
한 줄당 하나의 명령어를 등록
  -
각 필드의 의미는 다음과 같다.
------  -------- ---------------------------------------------------
필 드  의 미  범 위
------  -------- ---------------------------------------------------
첫번째  분    0-59
두번째  시    0-23
세번째  일    0-31
네번째  월    1-12
다섯번째 요일   0-7 (0 또는 7=일요일, 1=월, 2=화,...)
여섯번째 명령어  실행할 명령을 한줄로 쓴다.
------  -------- ---------------------------------------------------

  • 0,15,30,45 * * * * command
    -> 매 15분 마다 실행
  • 10 3 * * * command
    -> 03시 10분에 실행
  • 10 * 1 * * command
    -> 매월 1일 0시 10분에 실행
  • 10 14 * * 1 command
    -> 매주 월요일 14시 10분에 실행


    ################################################


    참고 : http://radiocom.kunsan.ac.kr/lecture/oracle/package/dbms_scheduler.html
             http://blog.daum.net/won-bo/16839169
             http://kwoncharlie.blog.me/10091007798


    dbms_scheduler를 이용한 스케쥴링

    begin
     dbms_scheduler.create_job(
      job_name => 'get_stats',
      job_type => 'executable',
      job_action => '/home/oracle/get_weekly_stats.sh',
      repeat_interval => 'FREQ=DAILY;BYHOUR=17;BYMINUTE=45',
      enabled => true,
      comments => 'LEVEL 0');
    end;
    /
  • begin
     dbms_scheduler.set_attribute(
      name => 'get_stats',
      attribute => 'raise_events',
      value => dbms_scheduler.job_started
      +dbms_scheduler.job_succeeded
      +dbms_scheduler.job_failed
      +dbms_scheduler.job_broken
      +dbms_scheduler.job_stopped
     );
    end;
    /
  • exec dbms_scheduler.run_job('get_stats');
  • exec dbms_scheduler.drop_job('get_stats');





Posted by 자수성가한 부자