So I just wanted to share this quick write up on how to stop and start your Oracle database and listener automatically on a Linux server. I use this approach for 10g, 11g, and 12c and so far I haven’t had any issues.

Oracle provides the means to do this using scripts called dbshut and dbstart that are located in your $ORACLE_HOME/bin directory. The above two scripts actually read the /etc/oratab file to find out which databases to stop and start.

So the first thing we need to do is edit the /etc/oratab file and make sure that the databases are set to stop and start when the dbshut and dbstart commands are passed. You do this by making sure the last letter is set to ‘Y’. If you have a specific database you don’t want the scripts to affect you put a ‘N’. See below for examples.

ora10g:/u01/app/oracle/product/10.2.0/dbhome_1:N
ora11g:/u01/app/oracle/product/11.2.0/dbhome_1:Y
ora12c:/u01/app/oracle/product/12.1.0/dbhome_1:Y

Next we will need to create a script to take advantage of the dbshut and dbstart commands when you are shutting down or rebooting the server. Create a file:

/etc/init.d/dbora

In the file paste the following:

#!/bin/sh
# chkconfig: 345 99 10
# description: Service to start and stop Oracle Database and Listener
#
# processname: oracle
# config: /etc/oratab
# pidfile: /var/run/oracle.pid

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

RETVAL=0
ORA_OWNER="oracle"
ORA_HOME="/u01/app/oracle/product/11.2.0/dbhome_1"

# See how we were called.

prog="oracle"

start() {
echo -n $"Starting $prog: "
su - $ORA_OWNER -c "$ORA_HOME/bin/dbstart $ORA_HOME"
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/dbora

return $RETVAL
}

stop() {
echo -n $"Stopping $prog: "
su - $ORA_OWNER -c "$ORA_HOME/bin/dbshut $ORA_HOME"
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -r /var/lock/subsys/dbora

return $RETVAL
}

restart() {
stop
start
}

case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
esac

exit $?

You need to make sure to change the script so that the ORA-OWNER and ORA_HOME match your configuration.

Next you need to run the following commands to change permissions and make the service active.

[root@oracle ~]# chgrp  dba /etc/init.d/dbora
[root@oracle ~]# chmod 750 /etc/init.d/dbora
[root@oracle ~]# chkconfig --level 345 dbora on

With that you should be able to now reboot your server and have the databases and listener(s) stop and start automatically.

Share This