2 Replies Latest reply: May 18, 2013 10:04 AM by TrevorWJNorth RSS

    Schell Script to Schdule In Dbms_Schedule

    705101
      Hi All,

      I've a script to schedule in dbms_scheduler, here is the usage of script in linux command line..

      "/home/oracle/scripts/run.sh table_name"

      Any inputs how to schedule it in dbms scheduler daily to run 1200 AM
        • 1. Re: Schell Script to Schdule In Dbms_Schedule
          sushaant
          You can refer to below blog for more clarification and examples.

          http://kamranagayev.wordpress.com/2009/02/23/using-oracle-utl_file-utl_smtp-packages-and-linux-shell-scripting-and-cron-utility-together-2/
          • 2. Re: Schell Script to Schdule In Dbms_Schedule
            TrevorWJNorth
            @sushaant - the post you refer to has no relevance to using DBMS_SCHEDULER - it is just using a cron job to run a shell script.

            @Ramu - if the shell script is on the same server as the oracle database, you need to do the following

            1) Create a credential that has the unix user you want to run the script as.
            Note the script itself MUST set up any environment needed for the job to run - DBMS_SCHEDULER will not execute the .profile of the user you supply in the credential so don't assume in run.sh that any $PATH will exist or any other environment variables, you must set these explicitly. E.g. if you use a unix command, you should ensure you specify the full path to it in run.sh.

            BEGIN
            DBMS_SCHEDULER.CREATE_CREDENTIAL('YOUR_CREDENTIAL', 'username', 'password');
            END;


            2) In sqlplus, create a job with an external job that references run.sh and add argument. Change yourjob to be the job name
            you want to see when the job runs and your_table_name to the table that should be used in this particular job.

            dbms_scheduler.create_program(
            job_name=>'yourprogram',
            job_type=>'EXECUTABLE',
            job_action=>'/home/oracle/scripts/run.sh',
            number_of_arguments=>1);


            4) Create a job which links the program to the schedule and runs daily at midday and create other jobs for other tables giving them different parameters. If you have several of these to run but don't want them to run at the same time, look at the CREATE_CHAIN commands in the Oracle 11gR2 database admin guide as there are two whole chapters (28 & 29) on the scheduler ( http://docs.oracle.com/cd/E11882_01/server.112/e25494/scheduse.htm#i1033533 )

            BEGIN
            DBMS_SCHEDULER.CREATE_JOB (
            job_name => 'run_tablename1',
            program_name => 'yourprogram',
            repeat_interval => 'FREQ=DAILY;BYHOUR=12',
            credential_name => 'YOUR_CREDENTIAL',
            comments => 'Daily at noon');

            dbms_scheduler.set_job_argument_value@orl1(job_name=>'run_tablename1', argument_position=>1, argument_value='your_table_name');
            END;

            Note that as you are passing a table_name to the run.sh, i'm guessing it might be running some Oracle commands such as an analyse so if run.sh contains PLSQL commands, you could create a PLSQL package or procedure in the database and instead of creating an external program that runs a shells script, just run the PLSQL directly from DBMS_SCHEDULER. This is simpler as you don't need to create credentials etc.. needed for an external program.


            BEGIN
            DBMS_SCHEDULER.CREATE_PROGRAM (
            job_name => 'yourname',
            job_type => 'STORED_PROCEDURE',
            job_action => 'YOUR_PKG.DO_STUFF',


            start_date => '18-MAY-13 12.00.00 AM',
            repeat_interval => 'FREQ=DAILY', /* every day */
            end_date => '17-MAY-14 12.00.00 AM',
            auto_drop => FALSE,
            comments => 'My new job');
            END;

            dbms_scheduler.set_job_argument_value@orl1(job_name=>'yourjob',
            argument_position=>1, argument_value='your_table_name');

            and again create a chain if you want to run several when one completes.

            You need to select from various tables to see the result of the jobs and whether they succeed or not such as

            select * From user_scheduler_job_run_details where job_Name='run_tablename1'
            SELECT * FROM USER_SCHEDULER_JOB_LOG;
            select * from user_scheduler_running_jobs

            You can add email notifications using DBMS_SCHEDULER.ADD_JOB_EMAIL_NOTIFICATION

            See the admin guide for more details.