Auto Start/Shutdown Oracle Database 11g R2 on Linux

Installing Oracle Database 11g R2 on a Linux Server (you can check my previous post) does not provide the database the possibility to preform an automatic startup/shutdown when the system is starts or shutdown, we need to do it manually!

so here is a few steps to make it happen automatically.

The automatic startup and shutdown of the Oracle database can be achieved with the files dbstart and dbshut both provided by Oracle. These files rely on the existence of the file /etc/oratab to work.

The format of the /etc/oratab file is as follows:

SID:ORACLE_HOME:AUTO

or in my example:

ORCL:/u01/app/oracle/product/11.2.0/dbhome_1:Y

To start and stop the database when the machine comes up and goes down by modifying the startup routines for the Linux machine. This is quite easy, although I should point out here that this may change depending on which flavor of Linux (slackware, debian, redhat, etc). I will show examples which work for Oracle Linux 5.x. To modify these for your own flavor of Linux, please see your Linux documentation sets.

Note1: /etc/init.d is the official location for placing start up scripts and most, but not all distros follow this convention. /etc/rc.d/init.d is where Red Hat (which Oracle Linux is based on) places startup scripts, but in order to comply with modern convention, they make /etc/initd a symlink to /etc/rc.d/init.d.

Firstly, we need to create the script which will run dbshut and dbstart in the /etc/rc.d/init.d directory. Create the following file as /etc/init.d/oracle:

Note2: The parts in red are optional where if you like to have Oracle Enterprise Manager starting/shutting down with the system. Note2: If you copy and paste the script you may/will get errors because of the double quotation and the (-) chars, please read Mike’s comment

#!/bin/sh
#
# /etc/rc.d/init.d/oracle
# Description: Starts and stops the Oracle database, listeners and Enterprise Manager
# See how we were called.

case "$1" in
start)
echo "Starting Oracle"
echo "—————————————————-" >> /var/log/oracle
date +"! %T %a %D : Starting Oracle Databases as part of system up." >> /var/log/oracle
echo "—————————————————-" >> /var/log/oracle
echo -n "Starting Oracle Listeners: "
su - oracle -c "lsnrctl start" >> /var/log/oracle
echo "Done."
echo -n "Starting Oracle Databases: "
su - oracle -c dbstart >> /var/log/oracle
echo "Done."
echo -n "Starting Oracle Enterprise Manager: "
su - oracle -c "emctl start dbconsole" >> /var/log/oracle
echo "Done."
echo ""
echo "—————————————————-" >> /var/log/oracle
date +"! %T %a %D : Finished." >> /var/log/oracle
echo "—————————————————-" >> /var/log/oracle
touch /var/lock/subsys/oracle
;;
stop)
echo "Shutting Down Oracle"
echo "—————————————————-" >> /var/log/oracle
date +"! %T %a %D : Shutting Down Oracle Databases as part of system down." >> /var/log/oracle
echo "—————————————————-" >> /var/log/oracle
echo -n "Shutting Down Oracle Enterprise Manager: "
su - oracle -c "emctl stop dbconsole" >> /var/log/oracle
echo "Done."
echo -n "Shutting Down Oracle Listeners: "
su - oracle -c "lsnrctl stop" >> /var/log/oracle
echo "Done."
rm -f /var/lock/subsys/oracle
echo -n "Shutting Down Oracle Databases: "
su - oracle -c dbshut >> /var/log/oracle
echo "Done."
echo ""
echo "—————————————————-" >> /var/log/oracle
date +"! %T %a %D : Finished." >> /var/log/oracle
echo "—————————————————-" >> /var/log/oracle
;;
restart)
echo "Restarting Oracle"
echo "—————————————————-" >> /var/log/oracle
date +"! %T %a %D : Restarting Oracle Databases as part of system up." >> /var/log/oracle
echo "—————————————————-" >> /var/log/oracle
echo -n "Restarting Oracle Listeners: "
su - oracle -c "lsnrctl stop" >> /var/log/oracle
su - oracle -c "lsnrctl start" >> /var/log/oracle
echo "Done."
echo -n "Restarting Oracle Databases: "
su - oracle -c dbshut >> /var/log/oracle
su - oracle -c dbstart >> /var/log/oracle
echo "Done."
echo -n "Restarting Oracle Enterprise Manager: "
su - oracle -c "emctl stop dbconsole" >> /var/log/oracle
su - oracle -c "emctl start dbconsole" >> /var/log/oracle
echo "Done."
echo ""
echo "—————————————————-" >> /var/log/oracle
date +"! %T %a %D : Finished." >> /var/log/oracle
echo "—————————————————-" >> /var/log/oracle
touch /var/lock/subsys/oracle
;;
*)
echo "Usage: oracle {start|stop|restart}"
exit 1
esac

it’s important to remember to make the script executable by simply run the command:

$ chmod +x /etc/init.d/oracle

It is worth checking that this file actually correctly stops and starts the databases for your system. Check the log file, /var/log/oracle for error messages.

Once this script is working we need to create start and kill symbolic links in the appropriate runlevel directories /etc/rc.d/rcX.d.

The following commands will ensure that the databases will come up in runlevels 2,3,4 and 5:

$ ln -s /etc/rc.d/init.d/oracle /etc/rc.d/rc2.d/S99oracle
$ ln -s /etc/rc.d/init.d/oracle /etc/rc.d/rc3.d/S99oracle
$ ln -s /etc/rc.d/init.d/oracle /etc/rc.d/rc4.d/S99oracle 
$ ln -s /etc/rc.d/init.d/oracle /etc/rc.d/rc5.d/S99oracle

To stop the databases on reboot or restart we need the following links:

$ ln -s /etc/rc.d/init.d/oracle /etc/rc.d/rc0.d/K01oracle # Halting
$ ln -s /etc/rc.d/init.d/oracle /etc/rc.d/rc6.d/K01oracle # Rebooting

I use the method above and it works fine for me, no issues so far, also I have to add that I did not write the whole thing it’s based on my online resources that can be easily found by just searching

34 thoughts on “Auto Start/Shutdown Oracle Database 11g R2 on Linux

  1. Thanks for this. With as many detailed instructions there are for installing it, and for as much as it costs, you’d think they’d provide a script like this. After all, one might like to actually start the database now and then.

    Note: WordPress turned the quotation marks in your script into fancy quotes rather than plain ascii ones, and thus copying and pasting it doesn’t work without alot of hand editing. The oracle commands are likely not included in one’s path, so I added a variable set to that and referenced it in front of all the commands

  2. Hi,

    Thanks for your post guidelines. But I have found some issues.

    “touch /var/lock/subsys/oracle”

    I can’t find the file. The file does not exist that the script file doesn’t work properly.

  3. Hi, I have adjust a little about system environment variables. But got the following error while try to execute the /etc/init.d/oracle script.

    ./oracle: line 71: restart}”: command not found
    stop: missing job name
    Try `stop –help’ for more information.

  4. First of all great article. It is a wonder oracle can’t seem to manage to put this in their own documentation.

    For those getting the error: ./oracle: line 71: restart}” This was caused by a copy paste issue with the doublequotes. Simply replace the open and close double quotes with standard ” quotes.

    After this I was getting the error: su: user – does not exist
    This was a copy paste issue as well. replace – with a standard – and then it should work like a charm.

  5. Thanks for your help!

    I have to notice that when I downloaded the script (by using Firefox 10.0.0.12, Oracle Linux 6.3) there was Carriage Return plus Linefeed at the end of each line. Unix systems use a Linefeed (\n, ASCII 10 or 0x0A), while Windows uses Carriage Return PLUS Linefeed (\r\n, ASCII 10,13 or 0x0D 0x0A). (Some help, how to remove CR from shell with tr will be highly appreciated 🙂 )

    Also, can we use “Create the following file as /etc/rc.d/init.d/oracle” instead “Create the following file as /etc/init.d/oracle”?

  6. Hi.. i have problem running oracle script

    [root@zmwlinux log]# service oracle start
    /etc/init.d/oracle: line 10: syntax error near unexpected token `Oracle”
    echo “—————————————————-” >> /var/log/oracle
    date + “!’
    /etc/init.d/oracle: line 10: `date + “! %T %a %D : Starting Oracle Databases as part of system up.” >> /var/log/oracle’

  7. Hi, thanks a lot for this post. however in script 6th line — case “$1” in — second double quotation still is a fancy quotes.

    • Hi, what type of errors?
      Wordpress change the chars specially the quotation 😦
      but I provided a link to the script file, please if you have issues keep me updated so I can fix the script, cheers 🙂

  8. I did it auto start and stop in this way

    1-Edit the #vi /etc/oratabfile :
    orcl:/home/oracle/app/oracle/product/11.2.0/dbhome_1:Y

    then

    vi /etc/init.d/oracle

    paste this script to it and save

    #!/bin/bash
    #
    # oracle This shell script takes care of starting and stopping
    # the Oracle subsystem (oracle).
    #
    # chkconfig: – 64 36
    # description: Oracle database server.
    # processname: oracle
    # pidfile: /var/run/oracle/oracle.pid
    ### BEGIN INIT INFO
    # Provides: oracle
    # Required-Start: $local_fs $remote_fs $network $named $syslog $time
    # Required-Stop: $local_fs $remote_fs $network $named $syslog $time
    # Short-Description: start and stop Oracle server
    # Description: Oracle database server
    ### END INIT INFO

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

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

    start() {
    if [ -e /var/lock/subsys/oracle ];then
    echo “Starting Oracle Enterprise Server”
    failure
    else
    echo “Starting Oracle Enterprise Server”
    success
    echo “`date “+%m/%d/%Y %H:%M:%S”` : Starting Oracle Listeners” >> /var/log/oracle/oracle.log
    su – oracle -c “lsnrctl start” >> /var/log/oracle/oracle.log
    echo “`date “+%m/%d/%Y %H:%M:%S”` : Starting Oracle Databases” >> /var/log/oracle/oracle.log
    su – oracle -c “dbstart” >> /var/log/oracle/oracle.log
    echo “`date “+%m/%d/%Y %H:%M:%S”` : Starting Oracle Enterprise Manager” >> /var/log/oracle/oracle.log
    su – oracle -c “emctl start dbconsole” >> /var/log/oracle/oracle.log
    touch /var/lock/subsys/oracle
    fi
    }

    stop() {
    if [ -f /var/lock/subsys/oracle ];then
    echo “Shutting Down Oracle Enterprise Server”
    success
    echo “`date “+%m/%d/%Y %H:%M:%S”` : Shutting Down Oracle Enterprise Manager” >> /var/log/oracle/oracle.log
    su – oracle -c “emctl stop dbconsole” >> /var/log/oracle/oracle.log
    echo “`date “+%m/%d/%Y %H:%M:%S”` : Shutting Down Oracle Listeners” >> /var/log/oracle/oracle.log
    su – oracle -c “lsnrctl stop” >> /var/log/oracle/oracle.log
    echo “`date “+%m/%d/%Y %H:%M:%S”` : Shutting Down Oracle Databases” >> /var/log/oracle/oracle.log
    su – oracle -c “dbshut” >> /var/log/oracle/oracle.log
    rm -f /var/lock/subsys/oracle
    else
    echo “Shutting Down Oracle Enterprise Server”
    failure
    fi
    }

    case “$1” in
    start)
    start
    ;;
    stop)
    stop
    ;;
    status)
    if [ -e /var/lock/subsys/oracle ]; then
    echo -n “Oracle Enterprise Server is Running”
    success
    echo
    else
    echo -n “Oracle Enterprise Server is Not Running”
    failure
    echo
    fi
    ;;
    restart)
    stop
    sleep 2
    start
    ;;
    *)
    echo $”Usage: $0 {start|stop|restart|status}”
    exit 1

    esac
    exit 0

    then
    chkconfig –add oracle
    chkconfig oracle on

    Cheers Your done

  9. Thanks for this script. Please can you tell me do i need to create a separate file to hold the symbolic link commands if yes where should i put the file. Sorry to be a pain i am new to oracle

    • hello,

      the commands below the script should do that for you 🙂

      ln -s /etc/rc.d/init.d/oracle /etc/rc.d/rc2.d/S99oracle
      ln -s /etc/rc.d/init.d/oracle /etc/rc.d/rc3.d/S99oracle
      ln -s /etc/rc.d/init.d/oracle /etc/rc.d/rc4.d/S99oracle
      ln -s /etc/rc.d/init.d/oracle /etc/rc.d/rc5.d/S99oracle
      ln -s /etc/rc.d/init.d/oracle /etc/rc.d/rc0.d/K01oracle
      ln -s /etc/rc.d/init.d/oracle /etc/rc.d/rc6.d/K01oracle

  10. In response to the win comment. There is still an error on the line 7:
    case “$1″ in. The ending doublequote is not correct. This is the origin of the error on line 10.

    • Thanks
      fixed, but you download the script from my Google Drive (link located above the script) to avoid any chars errors 🙂

  11. Hi SJ.. thanks for info.. how to do if we have two instances running on same server and single oracle home?

    • you do not have to do anything special
      just make sure in /etc/oratab it’s set to Y not N

      example: ORCL:/u01/app/oracle/product/11.2.0/dbhome_1:Y
      where:
      ORCL is the SID
      /u01/app/oracle/product/11.2.0/dbhome_1 is the Oracle Home
      and Y means the commands “dbstart” and “dbshut” will be affective
      we used these commands to auto start/shutdown the databases.

      hope that explains everything you need to know.

      • Thanks for quick reply SJ. script execute and db shutdown completed but having issues in startup. here is the oracle log under /var/log/

        âââââââââââââââââ-
        ! 16:19:51 Mon 05/18/15 : Shutting Down Oracle Databases as part of system down.
        âââââââââââââââââ-

        LSNRCTL for Linux: Version 11.2.0.3.0 – Production on 18-MAY-2015 16:19:51

        Copyright (c) 1991, 2011, Oracle. All rights reserved.

        Connecting to (ADDRESS=(PROTOCOL=IPC)(KEY=REGISTER))
        The command completed successfully

        ORACLE_HOME_LISTNER is not SET, unable to auto-stop Oracle Net Listener
        Usage: /apps/opt/oracle/product/11.2/db_1/bin/dbshut ORACLE_HOME
        Processing Database instance “nydev”: log file /apps/opt/oracle/product/11.2/db_1/shutdown.log
        Processing Database instance “mddev”: log file /apps/opt/oracle/product/11.2/db_1/shutdown.log
        âââââââââââââââââ-
        ! 16:20:12 Mon 05/18/15 : Finished.
        âââââââââââââââââ-
        âââââââââââââââââ-
        ! 16:21:42 Mon 05/18/15 : Starting Oracle Databases as part of system up.
        âââââââââââââââââ-

        LSNRCTL for Linux: Version 11.2.0.3.0 – Production on 18-MAY-2015 16:21:42

        Copyright (c) 1991, 2011, Oracle. All rights reserved.

        Starting /apps/opt/oracle/product/11.2/db_1/bin/tnslsnr: please wait…

        TNSLSNR for Linux: Version 11.2.0.3.0 – Production
        System parameter file is /apps/opt/oracle/product/11.2/db_1/network/admin/listener.ora
        Log messages written to /apps/opt/oracle/diag/tnslsnr/midd5/listener/alert/log.xml
        Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=REGISTER)))
        Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=midd5)(PORT=1521)))

        Connecting to (ADDRESS=(PROTOCOL=IPC)(KEY=REGISTER))
        STATUS of the LISTENER
        ————————
        Alias LISTENER
        Version TNSLSNR for Linux: Version 11.2.0.3.0 – Production
        Start Date 18-MAY-2015 16:21:42
        Uptime 0 days 0 hr. 0 min. 0 sec
        Trace Level off
        Security ON: Local OS Authentication
        SNMP OFF
        Listener Parameter File /apps/opt/oracle/product/11.2/db_1/network/admin/listener.ora
        Listener Log File /apps/opt/oracle/diag/tnslsnr/midd5/listener/alert/log.xml
        Listening Endpoints Summary…
        (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=REGISTER)))
        (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=midd5)(PORT=1521)))
        Services Summary…
        Service “mddev” has 1 instance(s).
        Instance “mdvdev”, status UNKNOWN, has 1 handler(s) for this service…
        Service “nynedev” has 1 instance(s).
        Instance “nydev”, status UNKNOWN, has 1 handler(s) for this service…
        The command completed successfully

        ORACLE_HOME_LISTNER is not SET, unable to auto-start Oracle Net Listener
        Usage: /apps/opt/oracle/product/11.2/db_1/bin/dbstart ORACLE_HOME
        Processing Database instance “nydev”: log file /apps/opt/oracle/product/11.2/db_1/startup.log
        Processing Database instance “mddev”: log file /apps/opt/oracle/product/11.2/db_1/startup.log
        âââââââââââââââââ-
        ! 16:21:42 Mon 05/18/15 : Finished.
        âââââââââââââââââ-

        We still use pfile to startup the instance since these are non prod db .

        all startup.log and shutdown.logs are 0 bytes.

        Is some thing wrong with my env setup for not starting up?

        Regards,
        Babu.

      • Thanks, When start os ,will database start automaticaly and shutdown os database stop atutomatically?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.