Automatically Startup and Shutdown Oracle Database on Linux

I found a script from oracle-base.com for starting database automatically when server is started. I tested this script on 11g single instance Oracle 11g database and RHEL operating systems.

This script must be run as a OS service so, we must create a service file under “init.d” directory. I named this file “orclScript”.

You can download thi scrpt from link below and upload it to your server’s “/etc/init.d” directory or you can copy script text and create new file by yourself.

orclScript.sh

[code language=”css”]
#!/bin/sh

# Set OS user of Oracle database to ORA_OWNER variable.
ORA_OWNER=oracle

case "$1" in
‘start’)
# Start the Oracle databases:
# The following command assumes that the oracle login
# will not prompt the user for any values
# Remove "&" if you don’t want startup as a background process.
su $ORA_OWNER -c "/home/oracle/dbscripts/startup.sh >> /home/oracle/dbscripts/orclScript.log 2>&1" &

touch /var/lock/subsys/orclScript
;;
‘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/dbscripts/shutdown.sh >> /home/oracle/dbscripts/orclScript.log 2>&1"
rm -f /var/lock/subsys/orclScript
;;
esac
[/code]

Give chmod 750 to this file.

chmod 750 /etc/init.d/orclScript

Add orclScript service to chkconfig.

chkconfig --add orclScript

This script runs startup and shutdown scripts. So let’s create this two scripts with oracle database OS user. Put this scripts under “dbscripts” directory.

# su - oracle
$ mkdir dbscripts

Now let’s create our startup and shutdown scripts.

startup.sh

[code language=”css”]
#!/bin/bash

export TMP=/tmp
export TMPDIR=$TMP
export ORACLE_HOSTNAME=orcl11g.localdomain
export ORACLE_HOME=/u01/app/oracle/product/11.2.0/db_home1
export PATH=$ORACLE_HOME/bin:/usr/sbin:/usr/local/bin:$PATH
export ORACLE_UNQNAME=orcl

export ORACLE_SID=orcl

# Start Listener
lsnrctl start

# Start Database
sqlplus / as sysdba << EOF
STARTUP;
EXIT;
EOF
[/code]

shutdown.sh

[code language=”css”]</pre>
#!/bin/bash

export TMP=/tmp
export TMPDIR=$TMP
export ORACLE_HOSTNAME=orcl11g.localdomain
export ORACLE_HOME=/u01/app/oracle/product/11.2.0/db_home1
export PATH=$ORACLE_HOME/bin:/usr/sbin:/usr/local/bin:$PATH
export ORACLE_UNQNAME=orcl

export ORACLE_SID=orcl

# Stop Database
sqlplus / as sysdba << EOF
SHUTDOWN IMMEDIATE;
EXIT;
EOF
[/code]

# Stop Listener
lsnrctl stop

We must give execution permission to these scripts.

[code language=”css”]
chmod u+x /home/oracle/dbscripts/startup.sh /home/oracle/dbscripts/shutdown.sh
[/code]

Our database will start when our server is started. Also we can use service commmand to start and shutdown the database.

[code language=”css”]
#service orclScript start
# service orclScript stop
[/code]

Hits: 3

Leave a Reply

Your email address will not be published. Required fields are marked *