Autostart your oracle database and listener on linux

3

Have you ever been wondering like me why DBCA doesn’t present you with the option, whilst creating your single instance database, to have it autostarted upon system boot? I mean like, what’s the point in (re)booting your database server, and not have it autostart the database(s) and listener running on it as well? They should have never been down in the first place!

It’s an easy fix though, you just need to call the the dbstart script in your $ORACLE_HOME/bin directory whilst the system is booting, which can be done by creating a service. To do this, create the below script called /etc/init.d/oracledb, as user root:


#!/bin/bash
#
# oracledb - little shell script to autostart your oracle database(s) and listener
#
# chkconfig: 2345 90 90

# Source function library.
. /etc/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

prog=oracledb
lockfile=/var/lock/subsys/$prog
ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1

start() {
[ "$NETWORKING" = "no" ] && exit 1
echo -n "Starting $prog: "
daemon --user=oracle "$ORACLE_HOME/bin/dbstart $ORACLE_HOME > /dev/null 2>&1"
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch $lockfile
return $RETVAL
}

stop() {
echo -n "Stopping $prog: "
daemon --user=oracle "$ORACLE_HOME/bin/dbshut $ORACLE_HOME > /dev/null 2>&1"
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f $lockfile
return $RETVAL
}

# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 2
esac

 

Next, set the correct permissions:


chmod 755 /etc/init.d/oracledb

And finally, to make the script execute during system boot:


chkconfig –-add oracledb

The chkconfig line of the script above, although it appears to be commented out, actually serves as input to determine to which run levels, and at what stage, the script should be executed. In this case it is runlevels 2345, and start scripts are added at position 90. Kill scripts are added to level 0 (shutdown), level 1 (single user mode) and level 6 (reboot). You can easily check this by using for example the following command:


[root@cc12c ~]# find /etc -name "*oracledb*" -exec ls -l {} \; |sort
lrwxrwxrwx 1 root root 18 Jan 13 16:09 /etc/rc.d/rc0.d/K90oracledb -> ../init.d/oracledb
lrwxrwxrwx 1 root root 18 Jan 13 16:09 /etc/rc.d/rc1.d/K90oracledb -> ../init.d/oracledb
lrwxrwxrwx 1 root root 18 Jan 13 16:09 /etc/rc.d/rc2.d/S90oracledb -> ../init.d/oracledb
lrwxrwxrwx 1 root root 18 Jan 13 16:09 /etc/rc.d/rc3.d/S90oracledb -> ../init.d/oracledb
lrwxrwxrwx 1 root root 18 Jan 13 16:09 /etc/rc.d/rc4.d/S90oracledb -> ../init.d/oracledb
lrwxrwxrwx 1 root root 18 Jan 13 16:09 /etc/rc.d/rc5.d/S90oracledb -> ../init.d/oracledb
lrwxrwxrwx 1 root root 18 Jan 13 16:09 /etc/rc.d/rc6.d/K90oracledb -> ../init.d/oracledb
-rwxr-xr-x 1 root root 885 Jan 13 16:09 /etc/rc.d/init.d/oracledb
[root@cc12c ~]#

 

You can now also call this service as the root user whilst your system is running, it will stop and start all databases listed in /etc/oratab, that have a Y listed for their last column:

 

image

And one more thing, in case you’re running your database(s) inside a local VM, which makes convenience far more of an issue than security, I always edit the /etc/sudoers file as well. This way, I can become the root user without having to enter a password, which would just be a waste of time otherwise:

image

In case you didn’t know how to configure this, become the root user, and do:


visudo –f /etc/sudoers

and add the following line to this file:


oracle    ALL=(ALL)     NOPASSWD:ALL

Now let’s just hope this will be an option in DBCA sometime soon, to be able to run some predefined script as the root user after creating your enterprise database, to have it autostart upon system boot.

Share.

About Author

3 Comments

Leave a Reply