In this article, I will share linux scripts with you for staring database automatically when operating system is started.
My environment is:
Oracle 11.2.0.4 database on Oracle Enterprise Linux 6.5
First of all, we must set Oracle environment variables in a profile file.
1 2 3 4 5 6 |
[oracle@databaseserver ~]$ pwd /home/oracle [oracle@databaseserver ~]$ cat .orcl export ORACLE_HOME=/u01/app/oracle/product/11.2.0.4/db export ORACLE_SID=orcl export PATH=$ORACLE_HOME/bin:$PATH |
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”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#!/bin/sh ## It is assumed that your database installation user is oracle case "$1" in 'start') su oracle -c "/home/oracle/dbscripts/startup.sh >> /home/oracle/dbscripts/orclScript.log 2>&1" & touch /var/lock/subsys/orclScript ;; 'stop') su oracle -c "/home/oracle/dbscripts/shutdown.sh >> /home/oracle/dbscripts/orclScript.log 2>&1" rm -f /var/lock/subsys/orclScript ;; esac |
Give chmod 750 to this file.
1 |
chmod 750 /etc/init.d/orclScript |
Add orclScript service to chkconfig.
1 |
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.
1 2 |
# su - oracle $ mkdir dbscripts |
Now let’s create our startup and shutdown scripts.
/home/oracle/dbscripts/startup.sh
1 2 3 4 5 6 7 8 9 10 11 12 |
#!/bin/bash . /home/oracle/.orcl # Start Listener lsnrctl start # Start Database sqlplus / as sysdba << EOF STARTUP; EXIT; EOF |
/home/oracle/dbscripts/shutdown.sh
1 2 3 4 5 6 7 8 9 10 11 12 |
#!/bin/bash . /home/oracle/.orcl # Stop Database sqlplus / as sysdba << EOF SHUTDOWN IMMEDIATE; EXIT; EOF # Stop Listener lsnrctl stop |
We must give execution permission to these scripts.
1 |
$ chmod u+x /home/oracle/dbscripts/startup.sh /home/oracle/dbscripts/shutdown.sh |
Our database will start when our server is started. Also we can use service commmand to start and shutdown the database.
1 2 |
# service orclScript start # service orclScript stop |