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
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 ]
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
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
hi
now it’s working fine… shall i use this scripts & procedures for rhel 6.2 also?
Yes, they should work for RHEL 6.2 too!
Good luck,
Matthias
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.
Oops! Thanks for letting me know! I will update this in my blog post and test it out myself…
Thanks,
Matthias
Thanks, worked like a charm
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..
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?
Very well written; easy to understand with lots of examples….. and it worked the first time :-)
Thanks
Thanks!
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!
Hey, thanks for the nice words!
Matthias
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?
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
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.
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
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
The script looks ok. Hmm… does it ever get past the first exit block? Any errors in /var/log/messages?
A bit puzzled here.
Matthias
Pingback: Confluence: SERES2
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
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.
Thanks for the feedback, Stephen! It’s true what you write about the dbconsole – it can gets messy when running multiple dbconsoles on one server…
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.
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.
Hmm… There’s probably something wrong with the installation of the dbconsole? What if you do (with user oracle): emctl start dbconsole?
This works. thanks buddy
Pingback: BO 4.1 on Linux – Part 1 – Install Oracle Database – busintblog
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,
What if you create a script for the iscsi initiation under /etc/init.d?
Sorry I now understand your question better – I believe you need a .depend.start file for that. Maybe check the init.d documentation.
Awesome man, Thanks… working perfectly as shown… :-)
Pingback: Automatically start Oracle 11g on Oracle Linux 6.2 after server reboot | IAM Consultant
Fantastic. Way better and simpler than the method in the Oracle manual (using links in the init.rc.d). Thanks so much!!
even when i try to execute as root it says -bash: /home/oracle/scripts/ora_star.sh
: permission denied
!!!
Hmm, this is strange. Must be something wrong with your permissions? Do you have execute permissions on the ora_star.sh file?