Batch Jobs That Follow the Database: Oracle Scheduler with RAC Services Across a Cross-Cloud Data Guard Pair
Many databases still depend on a crontab. A shell script on node 1 runs the nightly aggregation at two in the morning, another purges old rows at three, and a third exports a file for the finance team. It works until node 1 is down for patching, or until the database switches over to its standby in another cloud, where no crontab exists. Then the jobs silently stop, and the first sign is a missing report.
Oracle Scheduler keeps jobs inside the database, so they travel with it: they are part of the data that Data Guard ships to the standby. Combined with a RAC service, a job runs only on the instance where its service runs, moves when the service moves, and runs on whichever database is currently the primary. This post moves three cron jobs into the Scheduler on Exadata Database Service in Oracle Database@Azure, with a physical standby in OCI.
The Use Case
The primary SALES_AZ runs on a two-node Exadata VM cluster in Oracle Database@Azure. The standby SALES_OCI runs on Exadata Database Service in OCI. Three batch jobs run from the crontab of node 1 and call SQL*Plus scripts. During last month's switchover rehearsal, none of them ran, because the crontab only exists on the old primary. The goal is to run them inside the database, on node 2 by preference to keep node 1 free for online users, and on the new primary after any switchover.
Environment used in this runbook
Primary: SALES_AZ, Oracle Database 19c (19.24), two-node RAC on Exadata Database Service in Oracle Database@Azure
Standby: SALES_OCI, two-node RAC on Exadata Database Service in OCI, managed by the Data Guard broker
PDB: SALESPDB, application schema SALES
Jobs: nightly aggregation, purge of old order history, finance export
Licence: Oracle Scheduler and RAC services are part of Oracle Database Enterprise Edition; RAC itself is included in Exadata Database Service
How the Pieces Fit Together
A service decides where work runs. A RAC service has preferred and available instances, and Clusterware starts it on a preferred instance and relocates it if that instance fails.
A role-based service decides on which database. With the role PRIMARY, the service only starts on the database that currently has the primary role.
A job class links jobs to a service. Jobs in a job class with a service attribute run only on instances where that service is running.
Jobs live in the database. Their definitions and run history are ordinary data, replicated to the standby by redo, so they exist on the new primary after a switchover.
Step 1: List the Jobs That Run from Cron
Start with an inventory of what the crontab does today, so nothing is forgotten during the move.
Shell |
# On node 1 of the primary, as the oracle user crontab -l | grep -v '^#' |
Expected output |
0 2 * * * /home/oracle/batch/daily_aggregate.sh >> /home/oracle/batch/log/daily_aggregate.log 2>&1
0 3 * * * /home/oracle/batch/purge_order_history.sh >> /home/oracle/batch/log/purge.log 2>&1
30 6 * * 1-5 /home/oracle/batch/finance_export.sh >> /home/oracle/batch/log/finance_export.log 2>&1What to expect: Three entries: an aggregation every night at 02:00, a purge at 03:00, and a finance export on weekdays at 06:30. Open each script to find the PL/SQL or SQL it really runs; in this case each one calls a procedure in the SALES schema, which makes the move to the Scheduler straightforward.
Step 2: Create a Role-Based Batch Service on Both Databases
Create a service for batch work that prefers instance 2 and can fail over to instance 1, and that only runs on the primary. Create it with the same name on the standby, so it starts there after a switchover.
Shell |
# On the primary cluster srvctl add service -db SALES_AZ -pdb SALESPDB -service sales_batch \ -preferred SALES2 -available SALES1 -role PRIMARY -policy AUTOMATIC srvctl start service -db SALES_AZ -service sales_batch
# On the standby cluster: same service, started only if this database becomes primary srvctl add service -db SALES_OCI -pdb SALESPDB -service sales_batch \ -preferred SALES2 -available SALES1 -role PRIMARY -policy AUTOMATIC
srvctl status service -db SALES_AZ -service sales_batch |
Expected output |
Service sales_batch is running on instance(s) SALES2What this does: On SALES_AZ, the service runs on instance SALES2. If SALES2 goes down, Clusterware relocates it to SALES1. On SALES_OCI, the service is defined but stays down while that database is a standby; after a switchover, the broker and Clusterware start it there because its role is then PRIMARY.
Step 3: Create a Job Class Bound to the Service
Create a job class that points to the batch service. Every job placed in this class runs only where the service runs.
SQL |
-- Connected to SALESPDB as a DBA BEGIN DBMS_SCHEDULER.CREATE_JOB_CLASS( job_class_name => 'SALES_BATCH_CLASS', service => 'sales_batch', logging_level => DBMS_SCHEDULER.LOGGING_FULL, log_history => 60, comments => 'Nightly batch, runs where the sales_batch service runs'); END; /
GRANT EXECUTE ON SYS.SALES_BATCH_CLASS TO sales;
COLUMN job_class_name FORMAT A18 COLUMN service FORMAT A12 COLUMN logging_level FORMAT A13
SELECT job_class_name, service, logging_level, log_history FROM dba_scheduler_job_classes WHERE job_class_name = 'SALES_BATCH_CLASS'; |
Expected output |
PL/SQL procedure successfully completed.
Grant succeeded.
JOB_CLASS_NAME SERVICE LOGGING_LEVEL LOG_HISTORY
------------------ ------------ ------------- -----------
SALES_BATCH_CLASS sales_batch FULL 60What this does: It creates the class in the SYS schema, which is where job classes always live, and lets the SALES schema use it. Full logging keeps the run details of each job for 60 days, which replaces the log files the cron scripts wrote.
Step 4: Create the Jobs
Create the three jobs as the SALES user, in the new job class, with schedules that match the crontab. The calendar syntax of the Scheduler replaces the cron fields.
SQL |
-- Connected to SALESPDB as SALES BEGIN DBMS_SCHEDULER.CREATE_JOB( job_name => 'DAILY_AGGREGATE', job_type => 'STORED_PROCEDURE', job_action => 'SALES.BATCH_PKG.DAILY_AGGREGATE', job_class => 'SALES_BATCH_CLASS', repeat_interval => 'FREQ=DAILY;BYHOUR=2;BYMINUTE=0;BYSECOND=0', enabled => TRUE);
DBMS_SCHEDULER.CREATE_JOB( job_name => 'PURGE_ORDER_HISTORY', job_type => 'STORED_PROCEDURE', job_action => 'SALES.BATCH_PKG.PURGE_ORDER_HISTORY', job_class => 'SALES_BATCH_CLASS', repeat_interval => 'FREQ=DAILY;BYHOUR=3;BYMINUTE=0;BYSECOND=0', enabled => TRUE);
DBMS_SCHEDULER.CREATE_JOB( job_name => 'FINANCE_EXPORT', job_type => 'STORED_PROCEDURE', job_action => 'SALES.BATCH_PKG.FINANCE_EXPORT', job_class => 'SALES_BATCH_CLASS', repeat_interval => 'FREQ=WEEKLY;BYDAY=MON,TUE,WED,THU,FRI;BYHOUR=6;BYMINUTE=30;BYSECOND=0', enabled => TRUE); END; / |
Expected output |
PL/SQL procedure successfully completed.What this does: It creates and enables the three jobs. The schedules follow the database time zone of the Scheduler, given by the DEFAULT_TIMEZONE attribute; check it with DBMS_SCHEDULER.GET_SCHEDULER_ATTRIBUTE, because a cloud database often runs in UTC while the crontab ran in local time.
Tip: Disable the crontab entries in the same change, by commenting them out with a note pointing to the new jobs. Two copies of the same purge running in parallel is a worse problem than one missing run.
Step 5: Check the Schedules
Confirm that the jobs are enabled, belong to the right class, and have the next run dates you expect.
SQL |
COLUMN job_name FORMAT A20 COLUMN job_class FORMAT A18 COLUMN next_run FORMAT A16
SELECT job_name, job_class, enabled, state, TO_CHAR(next_run_date, 'YYYY-MM-DD HH24:MI') AS next_run FROM dba_scheduler_jobs WHERE owner = 'SALES' ORDER BY next_run_date; |
Expected output |
JOB_NAME JOB_CLASS ENABL STATE NEXT_RUN
-------------------- ------------------ ----- --------------- ----------------
DAILY_AGGREGATE SALES_BATCH_CLASS TRUE SCHEDULED 2026-11-27 02:00
PURGE_ORDER_HISTORY SALES_BATCH_CLASS TRUE SCHEDULED 2026-11-27 03:00
FINANCE_EXPORT SALES_BATCH_CLASS TRUE SCHEDULED 2026-11-27 06:30What to expect: Three scheduled jobs with the next runs tomorrow at 02:00, 03:00 and 06:30. If a next run time is off by one or two hours, the Scheduler's default time zone differs from the one the crontab used; set it with DBMS_SCHEDULER.SET_SCHEDULER_ATTRIBUTE('default_timezone', 'Europe/Warsaw') or give each schedule an explicit start date with a time zone.
Step 6: Run a Job Once and Check Where It Ran
Run one job now, in the background as the Scheduler would, and check the run details: status, duration and the instance it ran on.
SQL |
BEGIN DBMS_SCHEDULER.RUN_JOB(job_name => 'SALES.DAILY_AGGREGATE', use_current_session => FALSE); END; /
COLUMN job_name FORMAT A16 COLUMN status FORMAT A10 COLUMN duration FORMAT A14
SELECT job_name, status, instance_id, TO_CHAR(actual_start_date, 'YYYY-MM-DD HH24:MI') AS started, TO_CHAR(run_duration) AS duration FROM dba_scheduler_job_run_details WHERE owner = 'SALES' AND job_name = 'DAILY_AGGREGATE' ORDER BY actual_start_date DESC FETCH FIRST 1 ROWS ONLY; |
Expected output |
PL/SQL procedure successfully completed.
JOB_NAME STATUS INSTANCE_ID STARTED DURATION
---------------- ---------- ----------- ---------------- --------------
DAILY_AGGREGATE SUCCEEDED 2 2026-11-26 16:12 +000 00:04:18What to expect: The job succeeded on instance 2, where the sales_batch service runs, in about four minutes. With use_current_session set to FALSE, the job runs through the Scheduler exactly as it will at night, including the job class and its service, so this is a fair test.
Step 7: Watch the Jobs Follow a Node Failure
During a maintenance window, stop instance 2 and confirm that the service moves to instance 1 and that the next job run goes with it.
Shell |
srvctl stop instance -db SALES_AZ -instance SALES2 -force srvctl status service -db SALES_AZ -service sales_batch
sqlplus -s sales@salespdb_admin <<'EOF' EXEC DBMS_SCHEDULER.RUN_JOB('SALES.DAILY_AGGREGATE', use_current_session => FALSE) SELECT instance_id, status FROM user_scheduler_job_run_details WHERE job_name = 'DAILY_AGGREGATE' ORDER BY actual_start_date DESC FETCH FIRST 1 ROWS ONLY; EOF
srvctl start instance -db SALES_AZ -instance SALES2 |
Expected output |
Service sales_batch is running on instance(s) SALES1
PL/SQL procedure successfully completed.
INSTANCE_ID STATUS
----------- ------------------------------
1 SUCCEEDEDWhat to expect: With SALES2 down, the service runs on SALES1, and the job ran there without any change to the job itself. After SALES2 starts again, the service stays on SALES1 until it is relocated, for example with srvctl relocate service, so plan that as part of the maintenance runbook.
Step 8: Confirm the Jobs After a Switchover
In the next switchover rehearsal, check on the new primary in OCI that the service started and that the jobs are there, scheduled and ready to run.
Shell |
dgmgrl sys@SALES_AZ "switchover to 'SALES_OCI'"
# On a database server of the OCI cluster, now the primary srvctl status service -db SALES_OCI -service sales_batch
sqlplus -s / as sysdba <<'EOF' ALTER SESSION SET CONTAINER = salespdb; SELECT job_name, state FROM dba_scheduler_jobs WHERE owner = 'SALES' ORDER BY job_name; EOF |
Expected output |
Switchover succeeded, new primary is "sales_oci"
Service sales_batch is running on instance(s) SALES2
JOB_NAME STATE
------------------------------ ---------------
DAILY_AGGREGATE SCHEDULED
FINANCE_EXPORT SCHEDULED
PURGE_ORDER_HISTORY SCHEDULEDWhat to expect: The batch service now runs on the new primary in OCI, and the three jobs are scheduled there, because their definitions came across with the data. On SALES_AZ, now the standby, the service is down, so nothing runs twice. This is the test the crontab failed last month.
Step 9: Monitor Failed Runs
Replace the old habit of reading log files with a daily query on failures, and hook it into the team's monitoring.
SQL |
COLUMN job_name FORMAT A20 COLUMN error# FORMAT 99999 COLUMN additional_info FORMAT A40
SELECT job_name, TO_CHAR(actual_start_date, 'YYYY-MM-DD HH24:MI') AS started, error#, SUBSTR(additional_info, 1, 40) AS additional_info FROM dba_scheduler_job_run_details WHERE owner = 'SALES' AND status <> 'SUCCEEDED' AND actual_start_date > SYSTIMESTAMP - INTERVAL '7' DAY ORDER BY actual_start_date; |
Expected output |
JOB_NAME STARTED ERROR# ADDITIONAL_INFO
-------------------- ---------------- ------ ----------------------------------------
PURGE_ORDER_HISTORY 2026-11-21 03:00 1652 ORA-01652: unable to extend temp segmentWhat to expect: One failed run in the last week, with its error: the purge ran out of temporary space. The Scheduler also offers e-mail notifications per job with DBMS_SCHEDULER.ADD_JOB_EMAIL_NOTIFICATION, once an SMTP server is set in the Scheduler attributes; without that, this query in the monitoring tool is enough.
Summary
Jobs that live in a crontab do not survive a node failure or a switchover; jobs that live in the database do. List the cron entries, create a role-based RAC service for batch work on both the primary and the standby, and bind a job class to it. Create the jobs in that class with Scheduler calendar syntax, check their next run dates and the Scheduler's time zone, and run each one once through the Scheduler. Then prove the design: stop the preferred instance and see the job move, switch over to the standby in the other cloud and see the jobs ready there, and monitor failed runs with DBA_SCHEDULER_JOB_RUN_DETAILS.
Comments