How to auto start oracle listener on Linux with machine restart?
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.
2 Answers 2
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
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
※ Login to the system as the owner of Oracle Home.
Precaution : If you start the listener first and start the database later then register the database to the listener. so it is recommended to plan the automatic start for both database and listener sequentially(Refer additional information).
Specify REGISTER to instruct the PMON background process to register the instance with the listeners immediately. If you do not specify this clause, then registration of the instance does not occur until the next time PMON executes the discovery routine. As a result, clients may not be able to access the services for as long as 60
seconds after the listener is started.
SQL Language Reference(Refer REGISTER Clause)
▼ Please follow below steps to start the listener automatically when OS is restarted
※ There is no specific document with related to the command below.So I have tested the results in the in verfication environment.
Step 1: Go to the (/home/oracle) location and you can see the «bash profile» file.
Example : $ls -la .bash_profile
Step 2: use the editor and add listener starting command statement «$ORACLE_HOME/bin/lsnrctl start LISTENER_NEW» at the end of the (.bash_profile) file. You can see the added statement below in the (.bash_profile) file.
Example : $ cat .bash_profile
$ORACLE_HOME : is the database software in your environment.
LISTENER_NEW : is the name of the listener created in your environment.
If you do not know the listener name, please check the file in the path below. /network/admin/listener.ora
Step 3: Reload the profile . Stop the listener and restart the server then you can observe that the listener is up and running
☆ Procedure to start DB and listener automatically when the OS starts: ============================================================================
Step 1: Update ‘oratab’ (under /etc or /var/opt/oracle) as:
If the above part is N, edit it to Y.Where Y states that the database can be started up and shutdown using dbstart/dbshut.
: is the DB instance name of the customer's environment. : where Database software is installed
Step 2.Create the service script at root user (/etc/init.d/dbora)
※ If you using non default listener then use listener name in the script like $ORA_HOME/bin/lsnrctl start
#description: Starts and stops Oracle processes
su — $ORA_OWNER -c «$ORA_HOME/bin/lsnrctl start»
su — $ORA_OWNER -c $ORA_HOME/bin/dbstart ;;
su — $ORA_OWNER -c «$ORA_HOME/bin/lsnrctl stop»
su — $ORA_OWNER -c $ORA_HOME/bin/dbshut ;;
Step 3: Set script permissions:
Example : # chmod 755 /etc/init.d/dbora
Step 4:Register the Service
Example : # /sbin/chkconfig —add dbora
Step 5:Check the configuration
Example : # systemctl enable dbora
Step 6: Restart the server
Step 7: To test the startup of your database and listener, issue the following command «as root»:
Example : # service dbora start (OR) # /etc/init.d/dbora start
■Reference: (Applicable to Oracle 19c also)
How to Automate Startup/Shutdown of Oracle Database on Linux (Doc ID 222813.1)
※ You can Register the service with systemd and implement procedure to start DB and listener automatically when the OS starts.(Doc ID 2229679.1 — Applicable to 19c also)
Step 1: Modify /etc/oratab and set the database to start automatically.
If the above part is N, edit it to Y. : is the DB instance name of the customer's environment.
: Database software is installed in
Step 2: Create an environment definition file as a root user Write down the environment variables used by the listener and the Oracle database when the system starts.
Step 3: Create a systemd setting definition file as a root user, When the system starts, the database and listener «Set it to start automatically.» Database startup service «/usr/lib/systemd/system/oracledb.service» file like below example
Description=The Oracle Database Service After = network.target [Service] Type=forking EnvironmentFile=/etc/sysconfig/oracledb ExecStart=/bin/dbstart ExecStop=/bin/dbshut User=oracle [Install] WantedBy=graphical.target
Create the «/usr/lib/systemd/system/listener.service» file with settings like the example below. /////////////////Listener startup rvice/////////////////////////////
[Unit] Description=oracle net listener After=network.target
[Service] Type=forking EnvironmentFile=/etc/sysconfig/oracledb ExecStart=/bin/lsnrctl start ExecStop=/bin/lsnrctl stop User=oracle 3 [Install] WantedBy=graphical.target
★ WantedBy=graphical.target supports runlevel Please check the runlevel with the «who -r» command.
★ If you do not know the listener name, please check the file in the path /network/admin/listener.ora.
Step 4: Reload the created service settings.
Example : # systemctl daemon-reload
Step 5: Activate the created service.
Example : # systemctl enable listener.service oracledb.service
Step 6: Restart the server to verify that DB is up automatically
Automatic Stop of Database (dbshut) not working in OL 7 with systemd (Doc ID 2229679.1)
How to Create Port Listener in Windows or Linux– Handy for Connectivity Test
Invicti Web Application Security Scanner – the only solution that delivers automatic verification of vulnerabilities with Proof-Based Scanning™.
One of the challenging tasks while working in the project team is to perform the necessary connectivity test though services don’t exist.
This is often you have to do when you are working in a DMZ environment for migration or new projects. Let’s take a real-time example – you are working on migration, and you have to ensure connectivity exists between application “A” to “B” on a particular port.
Well. It’s straightforward you can perform telnet but how about when “B” doesn’t have any service running? That’s where you need the port listener to help in this situation.
If you have a similar situation or feel this would be beneficial for you at work, then here are few ways to achieve this in Windows or UNIX platform.
To create post listener in Windows OS
To have a port listener on a specific port in Windows, you can use the “Port Listener” utility.
This utility is available for free for Windows 95 to Windows 10.
- Download Post Listener as zip or exe format from here
- In this guide, I will download the exe format
- Double click on downloaded postlistener.exe file
- It will prompt to select the location to extract the files, click on unzip
- Double click on listener to start the utility
- Enter the port number which you want to test and click on start
- In the above example, I have started listening port on 5500, and it’s time to validate if it’s running.
Open a command prompt and run netstat to validate if port 5500 is listening
So yes, now I have created a port listener successfully in Windows.
To create post listener in Linux OS
The procedure is slightly different in Linux; here we will use netcat (nc) command to start the listener.
[root@Chandan ~]# netstat -anlp |grep 5500 tcp 0 0 0.0.0.0:5500 0.0.0.0:* LISTEN 21085/nc [root@Chandan ~]#
So here I have port 5500 listened successfully. Doing this in Linux is slightly more comfortable, isn’t it?
To create a port listener using Python
The above two examples are limited to OS. How about having a python script that can work on Windows or UNIX?
Well, I found the below python code which works on Windows and Linux both. Create a file – let’s say portlistener.py with below code
''' Simple socket server using threads ''' import socket import sys HOST = '' # Symbolic name, meaning all available interfaces PORT = 5500 # Arbitrary non-privileged port s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) print 'Socket created' #Bind socket to local host and port try: s.bind((HOST, PORT)) except socket.error as msg: print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1] sys.exit() print 'Socket bind complete' #Start listening on socket s.listen(10) print 'Socket now listening' #now keep talking with the client while 1: #wait to accept a connection - blocking call conn, addr = s.accept() print 'Connected with ' + addr[0] + ':' + str(addr[1]) s.close()
Save the file and run it with python command as shown below
[root@Chandan ~]# python portlistener.py Socket created Socket bind complete Socket now listening
Interested in learning Python? Check out this online course.
I hope the above procedure helps you to create a port listener for the connectivity tests.