Linux – How to auto start oracle listener on Linux with machine restart

linuxoracle

I've a Linux VM with Oracle database serve and it gets restarted periodically and hence next time, oracle listener is not running. So I've to start it manually with following commands –

export ORACLE_HOME=/home/oracle/app/oracle/product/11.2.0/dbhome_1
export PATH=$PATH:$ORACLE_HOME/bin
export ORACLE_SID=orcl;
$ORACLE_HOME/bin/lsnrctl start LISTENER

sqlplus
sys as sysdba/password
startup

This activity I've to do every time machine is restarted.

Is there a way with which oracle listener is started along with machine start? i.e. something like windows services which gets automatically started with machine start.

Best Answer

This question has been asked and answered hundreds, if not thousands, of times. A simple google search turns up dozens of hits. When in doubt, refer to the official oracle docs: https://docs.oracle.com/database/121/UNXAR/strt_stp.htm#UNXAR140

As an aside, there is no reason for you to manually enter all those commands to set the oracle environment. That is what the 'oraenv' utility is provided for. First, as a one-time setup, make sure your database is properly listed in /etc/oratab. Creating the database with dbca should have done this for you. Mine looks like this (the 'cat' command types the file, piping the output to 'grep -v' filters out the comment lines.

[oracle@vbol83-02 dba]$ cat /etc/oratab | grep -v ^#




cdb:/u01/app/oracle/product/19.0.0/dbhome_1:N
[oracle@vbol83-02 dba]$ 

The format of the entry is

oraclesid:oraclehome:Y|N

Now before invoking oraenv, let's check our enviornment:

[oracle@vbol83-02 dba]$ env|grep ORA
[oracle@vbol83-02 dba]$ echo $PATH
/usr/local/bin:/usr/sbin:/home/oracle/.local/bin:/home/oracle/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin
[oracle@vbol83-02 dba]$

The 'env' command shows all environment variables. Piping the output to 'grep' allows me to show only those that contain the string 'ORA'. And you see I have none. Also, you see that $ORACLE_HOME/bin is not in my PATH.

Now envoke oraenv. When prompted, enter the value of ORACLE_SID, as listed in /etc/oratab

[oracle@vbol83-02 dba]$ source oraenv
ORACLE_SID = [oracle] ? cdb
The Oracle base has been set to /u01/app/oracle
[oracle@vbol83-02 dba]$ 

Now let's check the environment again:

[oracle@vbol83-02 dba]$ env|grep ORA
ORACLE_SID=cdb
ORACLE_BASE=/u01/app/oracle
ORACLE_HOME=/u01/app/oracle/product/19.0.0/dbhome_1
[oracle@vbol83-02 dba]$ echo $PATH
/usr/local/bin:/usr/sbin:/home/oracle/.local/bin:/home/oracle/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/u01/app/oracle/product/19.0.0/dbhome_1/bin
[oracle@vbol83-02 dba]$ 

And you can see that 'oraenv' set it all for me.

I take it one step further, and put that in by .bash_logon script, so that my environment is set 'automagicly" when I log on to the server:

Relevent contents of my .bash_profile:

#----------------------------------------------------------------------
# 3 Aug 2012 - Ed Stevens
#   - replaced hard coded settings of ORACLE_* vars with use of oraenv
#     utility.
export ORACLE_TERM=xterm
export PATH=/usr/local/bin:/usr/sbin:$PATH
export ORACLE_SID=cdb
export ORAENV_ASK=NO
. oraenv
unset ORAENV_ASK