Tales From A Lazy Fat DBA

Its all about Databases & their performance, troubleshooting & much more …. ¯\_(ツ)_/¯

Oracle Database On Autopilot: A simple way to automatic restarts using Linux Services and Runlevels

Posted by FatDBA on March 3, 2024

Hello everyone,

Today’s post addresses a common request we’ve encountered in our careers— the need to automatically initiate the Oracle database upon the reboot of a Linux server. The objective is to ensure that all databases, listeners, and other dependent services are up and running seamlessly where there is no Oracle Restarts configured, eliminating the need for manual intervention. This post will delve into how we can achieve this efficiently and swiftly using Linux services. T

I am doing this test for Oracle 19c on RHEL7 but the steps are almost same for other DB or Types.

Step 1 : Enable your database entry in Oratab and make it ‘Y’.
Each line in the oratab file represents an Oracle Database instance, providing details about the Oracle home, the path to the instance’s data files, and whether the instance should be automatically started (Y) or not (N) during system startup.

Step 2 : Write initialization (INIT) script for starting the Oracle Database and its listener.
INIT scripts in Unix-like systems are used to start, stop, and restart services automatically during system boot or shutdown.

I have written a simple one to autostart database after OS reboots. You can modify it as pr your need and add more functionalities to it i.e. stop part etc.
You have to put your scripts under /etc/init.d/ directory.

In this case I have named the file as ‘oraclestartnew’

#!/bin/bash
# Author : Prashant 
# Purpose : This is a standard INIT script and is only for Oracle & Listener restart
# Next is the service priority runlevel startpriority stoppriority 
# chkconfig: 345 90 10
# Set Oracle environment variables
export ORACLE_HOME=/u01/app/oracle/product/19.0.0/dbhome_1
export ORACLE_OWNER=oracle
#LOG_FILE=/tmp/oraclestartup.log
# Start Oracle Database using dbstart
su - $ORACLE_OWNER -c "$ORACLE_HOME/bin/dbstart" > /dev/null 2>&1 &
su - $ORACLE_OWNER -c "$ORACLE_HOME/bin/lsnrctl start"  > /dev/null 2>&1 &

# Exit the script without waiting for the background process
exit 0		

Important to note is the chkconfig line which indicates the service priorities for different runlevels. In this case, the service should start in runlevels 3, 4, and 5 with a start priority of 90 and a stop priority of 10. Rest is simple shell script where I have set oracle variables and calling the dbshut and lsnrctl utility from ORACLE_HOME/bin and redirecting output to /dev/null to suppress any console output and finally exits the script. In short, this INIT script is designed to be run during system startup to start the Oracle Database and its listener in the background.

Step 3 : Now with the priorities mentioned, you can use commands like the following:

chkconfig --add oraclestartnew
chkconfig --level 345 oraclestartnew on
chkconfig --level 345 oraclestartnew 90 10

Alternatively you can create the symbolic links in different runlevel directories, connecting the /etc/init.d/oraclestartnew script to specific runlevels. These commands are typically used on Unix-like systems, such as Linux, to manage the execution of scripts during different stages of system startup.

[root@oracleontario init.d]#   ln -s /etc/init.d/oraclestartnew /etc/rc.d/rc3.d/S90oraclestartnew
[root@oracleontario init.d]# ln -s /etc/init.d/oraclestartnew /etc/rc.d/rc4.d/S90oraclestartnew
[root@oracleontario init.d]# ln -s /etc/init.d/oraclestartnew /etc/rc.d/rc5.d/S90oraclestartnew

These commands create symbolic links that start the oraclestartnew service with a priority of 90 during system startup.

For runlevel 0 (halt) and runlevel 6 (reboot):

[root@oracleontario init.d]#   ln -s /etc/init.d/oraclestartnew /etc/rc.d/rc0.d/K10oraclestartnew
[root@oracleontario init.d]# ln -s /etc/init.d/oraclestartnew /etc/rc.d/rc6.d/K10oraclestartnew

These commands create symbolic links that stop the oraclestartnew service with a priority of 10 during system shutdown or reboot. Now when the service is created, lets check its status.

[root@oracleontario ~]# chkconfig --list oraclestartnew
oraclestartnew 0:off 1:off 2:off 3:on 4:on 5:on 6:off

Cool, time to test! I have my database instance and listener is up and running, next I am going to issue reboot command and see if they comes back by its own.

[root@oracleontario init.d]# ps -ef|egrep 'tns|pmon'
root         14      2  0 11:10 ?        00:00:00 [netns]
oracle    48793      1  1 21:24 ?        00:00:00 /u01/app/oracle/product/19.0.0/dbhome_1/bin/tnslsnr LISTENER -inherit
oracle    49017      1  0 21:24 ?        00:00:00 ora_pmon_dixitdb
root      49396  25648  0 21:25 pts/1    00:00:00 grep -E --color=auto tns|pmon


[root@oracleontario init.d]# reboot
login as: root
root@192.168.68.73's password:
Last login: Thu Feb 29 17:21:02 2024 from 192.168.68.59


[root@oracleontario ~]#
[root@oracleontario ~]# ps -ef|egrep 'tns|pmon'
root         14      2  0 21:28 ?        00:00:00 [netns]
oracle     1817      1  0 21:28 ?        00:00:00 /u01/app/oracle/product/19.0.0/dbhome_1/bin/tnslsnr LISTENER -inherit
oracle     2291      1  0 21:29 ?        00:00:00 ora_pmon_dixitdb
root       2319   2123  0 21:29 pts/0    00:00:00 grep -E --color=auto tns|pmon
[root@oracleontario ~]#

Without any manual intervention, both the database instance and the listener automatically went online. You can deploy your OEM Agents or any other components in a similar manner, and they will autonomously come online after a system reboot.

Hope It Helped!
Prashant Dixit

Leave a comment