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.

Note: /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:

Note1: 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, here is the script with the correct chars.

#!/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

Advertisements
This entry was posted in Oracle and tagged , , , , , , . Bookmark the permalink.

31 Responses to Auto Start/Shutdown Oracle Database 11g R2 on Linux

  1. Dave W. says:

    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. Tim says:

    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. Tim says:

    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. oraenthu says:

    is the symbolic link right??

  5. Hi,

    Great script. However in the “restart” section it says dbstop instead of dbshut.

  6. Mike says:

    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.

  7. maxabg says:

    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”?

  8. win says:

    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’

  9. omid says:

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

  10. VK says:

    The script just has so many errors still now. Gosh!

    • SJ says:

      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 🙂

  11. bett says:

    hi, i still new in linux, just wanna know, how to test out the script?

  12. 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

  13. anthony adewumi says:

    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

    • SJ says:

      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

  14. alcubierre7 says:

    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.

    • SJ says:

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

  15. Jason says:

    You need to add oracle to service.

  16. Diego Rodrigo says:

    Works perfectly! Thanks a lot!

  17. Babu says:

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

    • SJ says:

      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.

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