Automatically start Oracle 11g on Oracle Linux 6.2 after server reboot

Environment: Oracle database 11.2.0.3.0, Oracle Linux 6.2

Oracle 11g includes 2 scripts which can be used to start or shut down Oracle databases on Linux. Both scripts are installed in $ORACLE_HOME/bin and are called dbstart and dbshut. However, these scripts are not executed automatically after you reboot your server. I will explain here how you can configure that.

First, you need to make sure that any database instances you want to autostart are set to “Y” in the /etc/oratab file. This is how mine looks like:

#

# This file is used by ORACLE utilities.  It is created by root.sh
# and updated by either Database Configuration Assistant while creating
# a database or ASM Configuration Assistant while creating ASM instance.

# A colon, ':', is used as the field terminator.  A new line terminates
# the entry.  Lines beginning with a pound sign, '#', are comments.
#
# Entries are of the form:
#   $ORACLE_SID:$ORACLE_HOME:<N|Y>:
#
# The first and second fields are the system identifier and home
# directory of the database respectively.  The third filed indicates
# to the dbstart utility that the database should , "Y", or should not,
# "N", be brought up at system boot time.
#
# Multiple entries with the same $ORACLE_SID are not allowed.
#
#
oratst:/u01/app/oracle/product/11.2.0/db_1:Y

The /etc/oratab file is normally created by running the root.sh script at the end of the installation. If you don’t have the file, you can always add it to your system by creating it manually (with user root!).

Next, we are going to create 2 scripts under /home/oracle/scripts: ora_start.sh and ora_stop.sh. These scripts will call dbstart and dbshut and will also allow us to add some more actions, for example the start of the Enterprise Manager database control or any other services you might have.

$ su – oracle

$ vi /home/oracle/scripts/ora_start.sh

#!/bin/bash

# script to start the Oracle database, listener and dbconsole

. ~/.bash_profile

# start the listener and the database
$ORACLE_HOME/bin/dbstart $ORACLE_HOME

# start the Enterprise Manager db console
$ORACLE_HOME/bin/emctl start dbconsole

exit 0

$ vi /home/oracle/scripts/ora_stop.sh

#!/bin/bash

# script to stop the Oracle database, listener and dbconsole

. ~/.bash_profile

# stop the Enterprise Manager db console
$ORACLE_HOME/bin/emctl stop dbconsole

# stop the listener and the database
$ORACLE_HOME/bin/dbshut $ORACLE_HOME

exit 0

You see that inside the scripts, we are calling the .bash_profile file of the user “oracle”. This is needed to set the ORACLE_HOME environment variable.

Next, give execute rights to the scripts:

$  chmod u+x ora_start.sh ora_stop.sh

You could now test these scripts to see if they correctly shut down and start up your Oracle database.

We will now create a wrapper script that can be used to schedule as a service.

With user root, create a file called “oracle” under /etc/init.d.

$ vi /etc/init.d/oracle

#!/bin/bash
# chkconfig: 345 99 10
# description: Oracle auto start-stop script.

# Set ORA_OWNER to the user id of the owner of the
# Oracle database in ORA_HOME.

ORA_OWNER=oracle
RETVAL=0

case "$1" in
    'start')
        # Start the Oracle databases:
        # The following command assumes that the oracle login
        # will not prompt the user for any values
        su - $ORA_OWNER -c "/home/oracle/scripts/ora_start.sh"
        touch /var/lock/subsys/oracle
        ;;
    'stop')
        # Stop the Oracle databases:
        # The following command assumes that the oracle login
        # will not prompt the user for any values
        su - $ORA_OWNER -c "/home/oracle/scripts/ora_stop.sh"
        rm -f /var/lock/subsys/oracle
        ;;
    *)
        echo $"Usage: $0 {start|stop}"
        RETVAL=1
esac
exit $RETVAL

$ chmod 750 /etc/init.d/oracle

To create a service of this script, run the following command:

$ chkconfig --add oracle

Next, check the script by running “service oracle stop” or “service oracle start” from the command line:

$ service oracle stop
Oracle Enterprise Manager 11g Database Control Release 11.2.0.3.0
Copyright (c) 1996, 2011 Oracle Corporation. All rights reserved.
Stopping Oracle Enterprise Manager 11g Database Control …
… Stopped.
Processing Database instance “oratst”: log file /u01/app/oracle/product/11.2.0/db_1/shutdown.log
$ service oracle start
Processing Database instance “oratst”: log file /u01/app/oracle/product/11.2.0/db_1/startup.log
Oracle Enterprise Manager 11g Database Control Release 11.2.0.3.0
Copyright (c) 1996, 2011 Oracle Corporation. All rights reserved.
Starting Oracle Enterprise Manager 11g Database Control …… started.

After this, it’s time for the final test: reboot your server and check if your Oracle database is automatically started after the reboot.

Good luck! :-)

Matthias

Advertisements

37 Responses to Automatically start Oracle 11g on Oracle Linux 6.2 after server reboot

  1. ramesh says:

    after stopping the service oracle, i couldn’t able to start the service oracle again..i’m getting the following error….

    [root@test1 ~]# service oracle start
    Processing Database instance “testdc”: log file /u01/app/oracle/product/11.2.0/db_1/startup.log
    Oracle Enterprise Manager 11g Database Control Release 11.2.0.3.0
    Copyright (c) 1996, 2011 Oracle Corporation. All rights reserved.
    Oracle Enterprise Manager 10g Database Control commands:
    emctl start | stop dbconsole
    emctl status | secure | setpasswd dbconsole
    emctl config dbconsole -heap_size -max_perm_size
    emctl status agent
    emctl status agent -secure [-omsurl <http://:/em/*>%5D
    emctl getversion
    emctl reload | upload | clearstate | getversion agent
    emctl reload agent dynamicproperties [:]….
    emctl config agent
    emctl config agent updateTZ
    emctl config agent getTZ
    emctl resetTZ agent
    emctl config agent credentials [[:]]
    emctl gensudoprops
    emctl clearsudoprops
    Blackout Usage :
    emctl start blackout [-nodeLevel] [[:]]…. [-d ]
    emctl stop blackout
    emctl status blackout [[:]]….

    The following are valid options for blackouts
    defaults to local node target if not specified.
    If -nodeLevel is specified after ,the blackout will be applied to all targets and any target list that follows will be ignored.
    Duration is specified in [days] hh:mm

    emctl getemhome
    emctl ilint
    Em Key Commands Usage :
    emctl config emkey -emkeyfile [-force] [-sysman_pwd ]
    emctl config emkey -emkey [-emkeyfile ] [-force] [-sysman_pwd ]
    emctl config emkey -repos [-emkeyfile ] [-force] [-sysman_pwd ]
    emctl config emkey -remove_from_repos [-sysman_pwd ]
    emctl config emkey -copy_to_repos [-sysman_pwd ]
    emctl status emkey [-sysman_pwd ]

    Secure DBConsole Usage :
    emctl secure dbconsole -sysman_pwd [-passwd_file ]
    [-host ] [-sid ] [-reset] [-secure_port ]
    [-cipher_suites ] [-sign_alg ]
    [-root_dc ] [-root_country ] [-root_state ] [-root_loc ]
    [-root_org ] [-root_unit ] [-root_email ]
    [-wallet ] [-wallet_pwd ] [-trust_certs_loc ]
    emctl secure status dbconsole
    Register Targettype Usage :
    emctl register oms targettype [-o ] OR
    emctl register oms targettype [-o ]

    • matthiashoys says:

      Hello,

      Looks like you’re passing a wrong parameter to the emctl command. How does your ora_start.sh script look like? Maybe you made a typo?

      Matthias

  2. ramesh says:

    Hi matthias,
    Thank u very much..i made a typo on ora_start.sh..listener & db starts automatically. but emctl is not working…follwing is the ora_start.sh script

    #!/bin/bash
    # script to start the Oracle database, listener and dbconsole
    . ~/.bash_profile
    # start the listener and the database
    $ORACLE_HOME/bin/dbstart $ORACLE_HOME
    # start the Enterprise Manager db console
    $ORACLE_HOME/bin/emctl start dbconsole
    exit 0

  3. ramesh says:

    hi

    now it’s working fine… shall i use this scripts & procedures for rhel 6.2 also?

  4. lefterhsts says:

    I had a problem with your /etc/init.d/oracle script.
    “service oracle stop” and “service oracle start” worked from command line, but although oracle was automatically starting, it was not shutting down.

    Spent a few hours and found that “touch /var/lock/subsys/dbora” and “rm -f /var/lock/subsys/dbora” was causing the problem.

    Since the service is named “oracle”, I changed those lines to “touch /var/lock/subsys/oracle” and “rm -f /var/lock/subsys/oracle” and now it works.

    OS is OEL 6.3.

  5. cattlespeed says:

    Thanks, worked like a charm

  6. Hi ,

    my machine got strucked during the start of oracle demon process. and unable to start further processss, please suggest me what i have to do..

    • matthiashoys says:

      Sorry, I don’t have enough information to help you with this. There are lots of things that could be wrong. Did you check the alert.log file for errors?

  7. tmack says:

    Very well written; easy to understand with lots of examples….. and it worked the first time :-)

    Thanks

  8. Inbal says:

    hi Matthias,
    i cannot but comment and say- this is one of the best posts i’ve read in who knows how many years, it’s really organized and helpful.

    thanks!

  9. Alan says:

    I have a question about the case “$1” in statement, it seems that when I reboot I don’t get past that because where is it supposed to get that variable from?

    • matthiashoys says:

      The $1 is the “start” or “stop” command parameters. Did you try to start or stop manually by running “oracle start” or “oracle stop”?

      Matthias

  10. Alan says:

    I got the issue with the case “$1” figured out and have everything setup as it is described above but the automatic startup doesn’t seem to work even though I can issue the service oradb stop and service oradb start and it works perfectly from the command line. I see the oradb in the appropriate rcX.d files for startup and shutdown depending on what init is issued so I’m stumped.

    • matthiashoys says:

      Did you name your script “oradb”? Then you need to give the same name to the file under /var/lock/subsys, did you modify that in the script?

      Matthias

  11. Alan says:

    Here is my dbora file in the /etc/init.d/ directory:

    ORA_HOME=/oracle/product/11.2.0/grid02
    ORACLE_UNQNAME=grid02
    ORA_OWNER=oracle
    export ORA_HOME ORACLE_UNQNAME ORA_OWNER

    if [ ! -f $ORA_HOME/bin/dbstart ]
    then
    echo “Oracle startup: cannot start”
    exit
    fi

    case “$1” in
    start)
    # Start the Oracle databases:
    # The following command assumes that the oracle login
    # will not prompt the user for any values
    su – $ORA_OWNER -c “$ORA_HOME/bin/lsnrctl start”
    su – $ORA_OWNER -c $ORA_HOME/bin/dbstart
    touch /var/lock/subsys/dbora
    ;;
    stop)
    # Stop the Oracle databases:
    # The following command assumes that the oracle login
    # will not prompt the user for any values
    su – $ORA_OWNER -c $ORA_HOME/bin/dbshut
    su – $ORA_OWNER -c “$ORA_HOME/bin/lsnrctl stop”
    rm -f /var/lock/subsys/dbora
    ;;
    esac

  12. Pingback: Confluence: SERES2

  13. Rahim says:

    Script is brilliant, Thankyou very much
    Only need db and listener as do not need dbconsole.
    Nice the way it all works around the profile :-)

    On RH6.4

  14. stephenemm says:

    As an oracle-on-linux noob, this really helped me out. One thing I found I had to do – because I’m running multiple instances on one host, each having an EM dbconsole – was loop over instances and start/stop the dbconsole for each:

    DBS=`cat /etc/oratab | grep -v “^#” | grep -v “N$” | cut -f1 -d: -s`

    for DB in $DBS
    do
    export ORACLE_UNQNAME=$DB
    export ORACLE_SID=$DB
    $ORACLE_HOME/bin/emctl stop dbconsole
    done

    The original scripts had me confused for a bit because the dbstart/dbshut scripts seem to work off of oratab directly (and start/stop all instances set to Y in oratab) whilst the emctl stop dbconsole works off of env vars that need setting per command exec.

  15. Jannat says:

    Hi Matthias,
    I am new with oracle database and trying to run it in my machine.
    I was trying to follow your scripts to run the service but every time I try to edit the ora_start.sh file it shows me an error.
    “ora_start.sh” E212: Can’t open file for writing.
    Could you please help me out regarding this issue.

    Regards,

    Jannat.

    • Jannat says:

      The problem was resolved but I am facing problem while stopping and starting the service.
      which says:
      $ service oracle stop
      OC4J Configuration issue. /u01/app/oracle/product/11.2.0/dbhome_1/oc4j/j2ee/OC4J_DBConsole_oracle11g.aamra.com_DB11G not found.
      Processing Database instance “oracle11”: log file /u01/app/oracle/product/11.2.0/dbhome_1/shutdown.log.

      • matthiashoys says:

        Hmm… There’s probably something wrong with the installation of the dbconsole? What if you do (with user oracle): emctl start dbconsole?

  16. thar says:

    This works. thanks buddy

  17. Pingback: BO 4.1 on Linux – Part 1 – Install Oracle Database – busintblog

  18. Nelson Chamba says:

    Hi Mathias,

    How do i put this command to start after the iscsi start because to my case my /u01/app… is located on the storage connected via iscsi.

    Regards,

  19. Pritiranjan says:

    Awesome man, Thanks… working perfectly as shown… :-)

  20. Pingback: Automatically start Oracle 11g on Oracle Linux 6.2 after server reboot | IAM Consultant

  21. jtw656 says:

    Fantastic. Way better and simpler than the method in the Oracle manual (using links in the init.rc.d). Thanks so much!!

  22. ahmedcse says:

    even when i try to execute as root it says -bash: /home/oracle/scripts/ora_star.sh
    : permission denied
    !!!

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: