Kamran Agayev’s Oracle Blog

Kamran Agayev’s Oracle Blog

Automatically StartUp and Shutdown an Oracle Database in Linux OS

Posted by Kamran Agayev A. on December 22, 2008

Today I want to show you how we can Start-up and Shutdown an Oracle database on Linux OS automatically
In Windows, when we install an Oracle database, it creates a Windows Service which starts the database automatically as soon as OS starts. But in Linux, it’s different. You need to create your own service to start and shutdown your database when you start and shutdown your OS respectively. Now we’ll go through this example step by step to show how we can do it

1. First of all we’ve to change our /etc/oratab file. Why do we use it? Oratab file is used by Oracle utilities. It’s created by root.sh script, when we install an Oracle Server, and is updated each time by DBCA (Database Configuration Assistant) when we create a new database. After having installed a new database (in this case, our database name is mydb), entering by root user, let us see the source of this file. At the end of this file, you’ll find:

mydb:/home/oracle/product/10.1.0/Db_1:N

The first field is the name of my database (mydb), the second one is my home directory (/home/oracle/product/10.1.0/Db_1), and the third indicates to the dbstart utility whether the database should, or should not be brough up at the system boot time with “Y” or “N” parameters respectively
As a root user, we’ll change last field of this line from “N” to “Y” to let dbstart utility start this database when it runs

2. In this step, we’ll create a file with name startdb and write a small script to be run when our system starts or shuts down. If you want this file to be added as a service which starts when your OS boots, you should put it in /etc/init.d/ directory. In this script, we’ll define two variables, ORA_OWNER and ORACLE_HOME and then, we’ll start (or shutdown) our database by connecting with oracle user and running lsnrctl (Listener utility), dbstart (utility which starts the database) and dbshut (utility which shutdowns the database) utilities depending on our OS state.
This is the source of our startdb file:

ORACLE_HOME=/home/oracle/product/10.1.0/Db_1
ORA_OWNER=oracle

case “$1” in
 ‘start’)  #If the system is starting, then …
su – $ORA_OWNER -c “$ORACLE_HOME/bin/lsnrctl start” #Start the listener
su – $ORA_OWNER -c “$ORACLE_HOME/bin/dbstart #Start the database
  ;;

‘stop’)   #If the system is stops, that is we’re shutting down our OS, then …
  su -$ORA_OWNER -c $ORACLE_HOME/bin/dbshut
  su -$ORA_OWNER -c “$ORACLE_HOME/bin/lsnrctl stop”
  ;;
esac

3. Once you’ve saved your file into this directory, make sure that it’s executable by running:
chmod 750  startdb

4. Then you need to add the appropriate symbolic links to cause the script to be executed when the system goes down, or comes up. Create it with ln -s command.

# ln -s /etc/init.d/startdb /etc/rc.d/rc3.d/K01startdb
# ln -s /etc/init.d/startdb /etc/rc.d/rc3.d/S99startdb
# ln -s /etc/init.d/startdb /etc/rc.d/rc5.d/K01startdb
# ln -s /etc/init.d/startdb /etc/rc.d/rc5.d/S99startdb

Let’s analyze these commands:
The first line creates symbolic link of our script in rc3.d directory. At startup, Linux runs /etc/rc.d/rc script at the current level (normally 3 or 5). rc3.d indicates 3rd runlevel, “K” indicates OS’s shutdown (on servers shutdown, Linux calls the scripts in /etc/rc.d/rc3/K* in order)
The second line creates another symbolic link of our script, to be run at startup of our OS, indicating “S99” and the name of our script
Another two lines creates symoblic links for 5th runlevel.

That’s all. Your script and your service is ready for use. Just restart your OS and upon startup you’ll see your newly created service running. Then open new terminal, enter you database and issue :
SQL>SELECT status FROM v$instance;

You’ll see your database’s status is OPEN

P.S
In some releases, even when we create a new service, it doesn’t work. When we issue dbstart command manually from OS, we’re getting an error:
cat: /var/opt/oracle/oratab: No such file or directory

It has simple reason and solution
If the directory /var/opt/oracle exists during the Oracle installation the dbhome script will have the parameter ORATAB set to “/var/opt/oracle/oratab” instead of “/etc/oratab”. The normal DBCA process during a default installation will still add the instance entry to /etc/oratab.

It has two solutions:
1. You can either copy the original oratab file to this directory :
cp /etc/oratab /var/opt/oracle/

2. Or you can edit dbstart and dbshut scripts, find the variable ORATAB, and you’ll find that it’s addressing to  /var/opt/oracle/oratab file. Just change it to /etc/oratab

19 Responses to “Automatically StartUp and Shutdown an Oracle Database in Linux OS”

  1. AJA said

    Would it not be tons easier to install the orarun RPM? This will create a file /etc/init.d/oracle and etc/sysconfig/oracle We use the sysconfig file to tell the init.d file which oracle products/services to start and how we want the kernel parameters set. I have found it most simple and reliable.

  2. You’re right AJA. It would be easier than doing all these steps. But I want to show how we can do it step by step by ourselfs and to show a little bit from Linux startup and shutdown process

  3. david said

    URGENT PLEASE!!

    I tried the above steps for Automatic StartUp and Shutdown an Oracle Database in my Linux OS and my database has refused to start since then.
    It displays the message below:

    ORA-00600: internal error code, arguments: [ keltnfy-ldmInit],[46],[1],[],[],[],[],[]

    please how do I recover my database

  4. Hi David
    Please for your Urgent questions, open an SR in metalink (contact Oracle Support)
    Or you can post a question in Oracle Forums

    For ORA-00600 errors (internal errors) you should open contact Oracle Support. There’s no any written documentation for such errors

  5. david said

    For the benefit of others. Below is the solution I found most valuable on yahoo search engine:

    ORA-00600 arguments: [keltnfy-ldmInit], [46], [1]
    Problem Description
    In oracle 10.2.0.1 while creating database with dbca it fails with message below.
    ORA-00600: internal error code, arguments: [keltnfy-ldmInit], [46], [1], [], [], [], [], []

    If you try to create your database manually then also the command startup nomount fails with above error message.

    Cause of the Problem
    In the error ldmInit indicates that the problem is related while getting host information of oracle during startup. The first argument 46 indicates the exception LDMERR_HOST_NOT_FOUND which is “gethostbyname system call fails”. Oracle was unable to get host information from OS and bug fires.

    Solution of the Problem
    Step 01: Check permission on /etc/hosts

    $ ls -l /etc/hosts-rw-r–r– 1 root root 153 Nov 24 2007 /etc/hosts

    Note that you need read permission of all users.

    Step 02: Check the contents of /etc/hosts
    Open the contents of /etc/hosts and check the contents inside it.
    $ less /etc/hosts
    Note that the contents of this files follow following format.

    IP Address fully qualified hostname simple or short hostname Alias

    A simple example,

    $ cat /etc/hosts# Do not remove the following line, or various programs# that require network functionality will fail.127.0.0.1 database localhost.localdomain localhost

    Step 03: Check the hostname and make sure you can ping your hostname.
    $ hostname
    database

    $ ping database
    PING database (127.0.0.1) 56(84) bytes of data.
    64 bytes from database (127.0.0.1): icmp_seq=0 ttl=64 time=0.057 ms
    64 bytes from database (127.0.0.1): icmp_seq=1 ttl=64 time=0.050 ms
    64 bytes from database (127.0.0.1): icmp_seq=2 ttl=64 time=0.041 ms

    If you get the following message,
    $ ping database
    ping: unknown host database

    then possibly you will hit above bug.
    And you need to modify /etc/hosts files. In the alias section you can give the name of your machine name. If your machine name is “database” you can give /etc/hosts entry as,

    127.0.0.1 database localhost.localdomain localhost

    And then ping database again. Make sure you are able to ping your host.

    Step 04: Diagnosis DNS problem if you have DNS setup
    If you have DNS setup, ping is not a tool to diagnose DNS problem. A better tool to use is nslookup, dnsquery, or dig.

    $nslookup http://www.google.com
    The forward and reverse lookup should succeed.

    Step 05: Check nsswitch.conf

    $ cat /etc/nsswitch.confhosts: files dns

    Make sure host lookup is also done through the /etc/hosts file and not just dns. The keyword files should come before dns.

    Step 06: Check resolv.conf

    $ cat /etc/resolv.confnameserver 4.2.2.2

  6. rohit said

    i tried all steps mentioned by u bt it did not work
    i was found again the same error
    plz tell me the best solution for this problem
    its very urgent

    thank u

  7. Hi Rohit

    The best solution is not to “Copy/paste” the script because there may be problem with commas and so on. So, just try to write down a script without copying it from web browser to your system

  8. Mu Savvy said

    Excellent job! Ordering the book now.

  9. Victor said

    Dear Kamran:

    I want to use the auto startup/shutdown process for my 11gR2 install on OEL. This worked fine on CentOS, but on OEL with 11g I get the following message when starting Enterprise Manager:

    [oracle@localhost ~]$ emctl start dbconsole
    OC4J Configuration issue. /u01/home/oracle/product/11.2.0/db_1/oc4j/j2ee/OC4J_DBConsole_localhost.localdomain_vv not found.

    Are there any changes or additional steps I need to do?

    Regards

  10. Are you able to start the EM from outside the script? If not, then try to delete and create the EM repository again

  11. Victor said

    Was only able to run EM upon initial startup of newly created database, after restarting OEL next day, couldn’t get EM to work.

    Are these the correct commands to delete & recreate EM repo?

    $ emca -deconfig dbcontrol db -repos drop

    $ emca -config dbcontrol db -repos create

  12. Victor said

    This worked:
    $ emca -deconfig dbcontrol db -repos drop
    $ emca -config dbcontrol db -repos recreate (& not create)

    When prompted for SYSMAN user, need to enter password in quotes “password” or you will continue to get invalid user/password prompt.

    also need to use the database control url given at the end of the enterprise manager configuration completed successfully message.

  13. tanveer said

    Where can I download orarun rpm?
    I tried to find that on internet but not find yet 😦

  14. Dear Tanveer

    You shouldn’t download any rpm to automate the startup and shutdown of the Oracle database. Just follow my steps and you will succeeed

  15. Dear Tanveer

    You shouldn’t download any rpm to automate the startup and shutdown of the Oracle database. Just follow my steps and you will succeeed

  16. tanveer said

    Dear Kamran,
    I have installed oracle10R2 on CentOs 5.6 and followed your steps to automate but it is not working 😦

  17. Dear Tanverr, just change the commas after copy/pasting the code and verify why it is not working. The code is correct

  18. tanveer said

    where sould I place commas ?

    ORACLE_HOME=/home/oracle/product/10.1.0/Db_1
    ORA_OWNER=oracle

    case “$1″ in
    ’start’) #If the system is starting, then …
    su – $ORA_OWNER -c “$ORACLE_HOME/bin/lsnrctl start” #Start the listener
    su – $ORA_OWNER -c “$ORACLE_HOME/bin/dbstart #Start the database
    ;;

    ‘stop’) #If the system is stops, that is we’re shutting down our OS, then …
    su -$ORA_OWNER -c $ORACLE_HOME/bin/dbshut
    su -$ORA_OWNER -c “$ORACLE_HOME/bin/lsnrctl stop”
    ;;
    esac

  19. erwin amin said

    Hello Kamran
    I got “Failed to auto-start Oracle Net Listener using /ade/vikrkuma_new/oracle/bin/tnslsnr”, when I start the script, “/etc/init.d/stardb start”
    but the oracle start successfully.

    To resolve this problem I got the answer from http://www.riccardoriva.com/archives/168.

    Anyway, your article is very helpful.

    thank you

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 )

Connecting to %s

 
%d bloggers like this: