Tuesday, August 12, 2025

Oracle Database Auto Start and Stop Script

  • Introduction: In Oracle Database, it is important to automatically start and stop the database services during server reboots or shutdowns to avoid manual work and ensure smooth operations. Oracle provides built-in scripts dbstart and dbshut for this purpose. By using these scripts with Linux services like systemd or init.d, we can make sure the database and listener start automatically when the server starts and shutdown properly during server shutdown. In this article, we will see how to implement this script step by step.

  • Prerequisites:
    • Must have root user or sudo access.
  • Environment:
Hostname orcl.oraeasy.com
OS OL9
Database Name ORCLDC
Database Version 19.27.0
Oracle Home /u01/app/oracle/product/19.0.0/dbhome_1

  • Approach: We will use Oracle provided script for auto startup (dbstart) and shutdown (dbshut) and those are located in $ORACLE_HOME/bin.

  • Now let's proceed to configure the script step by step:

  • 1. First we need to upadte the /etc/oratab file and make the Auto Start Option as Y, so that oracle provided script can be executed.
    
    
    [oracle@orcl ~]$ cat /etc/oratab
    #
    
    # 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::
    #
    # The first and second fields are the system identifier and home
    # directory of the database respectively.  The third field 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.
    #
    #
    orcldc:/u01/app/oracle/product/19.0.0/dbhome_1:Y
    [oracle@orcl ~]$
    
    
    2. Now create a script with name dbservice in /etc/rc.d/init.d from root user.
    
    
    [root@orcl ~]# cat /etc/rc.d/init.d/dbservice
     
    #!/bin/bash
    # chkconfig: 35 99 10
    # description: Starts and Stops Oracle database and Listener processes
    ORACLE_HOME=/u01/app/oracle/product/19.0.0/dbhome_1
    ORA_OWNER=oracle
    PATH=${PATH}:$ORACLE_HOME/bin
    HOST=`hostname`
    PLATFORM=`uname`
    export ORACLE_HOME PATH
    case "$1" in
      'start')
            echo -n $"Starting the Oracle Database: "
        su - $ORA_OWNER -c "$ORACLE_HOME/bin/lsnrctl start" &
        su - $ORA_OWNER -c "$ORACLE_HOME/bin/dbstart $ORACLE_HOME" &
       ;;
      'stop')
            echo -n $"Shutting down the Oracle Database: "
        su - $ORA_OWNER -c "$ORACLE_HOME/bin/lsnrctl stop" &
        su - $ORA_OWNER -c "$ORACLE_HOME/bin/dbshut $ORACLE_HOME" &
    ;;
    'restart')
            echo -n $"Shutting down the Oracle Database: "
              su - $ORA_OWNER -c "$ORACLE_HOME/bin/lsnrctl stop" &
              su - $ORA_OWNER -c "$ORACLE_HOME/bin/dbshut $ORACLE_HOME" &
            sleep 5
            echo -n $"Starting the Oracle Database: "
            su - $ORA_OWNER -c "$ORACLE_HOME/bin/lsnrctl start" &
            su - $ORA_OWNER -c "$ORACLE_HOME/bin/dbstart $ORACLE_HOME" &
      ;;
    *)
            echo "usage: $0 {start|stop|restart}"
            exit
    
       ;;
    esac
    
    # End of script dbservice
    [root@orcl ~]#
    
    
    3. Now change the permission for the file dbservice so that Oracle user can access that file.
    
    
    [root@orcl ~]# id oracle
    uid=54321(oracle) gid=54321(oinstall) groups=54321(oinstall),54322(dba),54323(oper),54324(backupdba),54325(dgdba),54326(kmdba),54330(racdba)
    [root@orcl ~]#
    [root@orcl ~]# chgrp oinstall /etc/init.d/dbservice
    [root@orcl ~]#
    [root@orcl ~]# chmod 750 /etc/init.d/dbservice
    
    
    4. Now register the dbservice to the Linux service management system and generates the required symbolic links within the /etc/rc.d directory.
    
    
    [root@orcl ~]# chkconfig --add dbservice
    [root@orcl ~]#
    [root@orcl ~]# cd /etc/rc.d
    [root@orcl rc.d]#
    [root@orcl rc.d]# ls
    init.d  rc0.d  rc1.d  rc2.d  rc3.d  rc4.d  rc5.d  rc6.d  rc.local
    [root@orcl rc.d]#
    [root@orcl rc.d]# cd rc3.d/
    [root@orcl rc3.d]#
    [root@orcl rc3.d]# ls -lrth
    total 0
    lrwxrwxrwx. 1 root root 19 Jul 15 22:21 S99dbservice -> ../init.d/dbservice
    [root@orcl rc3.d]#
    [root@orcl rc3.d]#  chkconfig --list | grep dbservice
    
    Note: This output shows SysV services only and does not include native
          systemd services. SysV configuration data might be overridden by native
          systemd configuration.
    
          If you want to list systemd services use 'systemctl list-unit-files'.
          To see services enabled on particular target use
          'systemctl list-dependencies [target]'.
    
    dbservice       0:off   1:off   2:off   3:on    4:off   5:on    6:off
    [root@orcl rc3.d]#
    
    
    5. Now enable the service.
    
    [root@orcl rc3.d]# systemctl status dbservice
    Unit dbservice.service could not be found.
    [root@orcl rc3.d]#
    [root@orcl rc3.d]# systemctl enable dbservice
    dbservice.service is not a native service, redirecting to systemd-sysv-install.
    Executing: /usr/lib/systemd/systemd-sysv-install enable dbservice
    
    [root@orcl rc3.d]#
    [root@orcl rc3.d]# systemctl status dbservice
    ○ dbservice.service - SYSV: Starts and Stops Oracle database and Listener processes
         Loaded: loaded (/etc/rc.d/init.d/dbservice; generated)
         Active: inactive (dead)
           Docs: man:systemd-sysv-generator(8)
    [root@orcl rc3.d]#
    
    
    6. Now ensure that SELINUX is set to permissive in /etc/selinux/config.
    
    [root@orcl ~]# cat /etc/selinux/config
    
    # This file controls the state of SELinux on the system.
    # SELINUX= can take one of these three values:
    #     enforcing - SELinux security policy is enforced.
    #     permissive - SELinux prints warnings instead of enforcing.
    #     disabled - No SELinux policy is loaded.
    # See also:
    # https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html/using_selinux/changing-selinux-states-and-modes_using-selinux#changing-selinux-modes-at-boot-time_changing-selinux-states-and-modes
    #
    # NOTE: Up to RHEL 8 release included, SELINUX=disabled would also
    # fully disable SELinux during boot. If you need a system with SELinux
    # fully disabled instead of SELinux running with no policy loaded, you
    # need to pass selinux=0 to the kernel command line. You can use grubby
    # to persistently set the bootloader to boot with selinux=0:
    #
    #    grubby --update-kernel ALL --args selinux=0
    #
    # To revert back to SELinux enabled:
    #
    #    grubby --update-kernel ALL --remove-args selinux
    #
    SELINUX=permissive
    # SELINUXTYPE= can take one of these three values:
    #     targeted - Targeted processes are protected,
    #     minimum - Modification of targeted policy. Only selected processes are protected.
    #     mls - Multi Level Security protection.
    SELINUXTYPE=targeted
    
    
    [root@orcl ~]#
    
    
    7. Now restart the server and check the database services.
    
    [root@orcl ~]# init 6
    [root@orcl ~]#
    Remote side unexpectedly closed network connection
    
    ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
    
    Session stopped
        - Press  to exit tab
        - Press R to restart session
        - Press S to save terminal output to file
        ┌──────────────────────────────────────────────────────────────────────┐
        │                 • MobaXterm Personal Edition v23.2 •                 │
        │               (SSH client, X server and network tools)               │
        │                                                                      │
        │ ⮞ SSH session to oracle@192.168.101.13                               │
        │   • Direct SSH      :  ✓                                             │
        │   • SSH compression :  ✓                                             │
        │   • SSH-browser     :  ✓                                             │
        │   • X11-forwarding  :  ✓  (remote display is forwarded through SSH)  │
        │                                                                      │
        │ ⮞ For more info, ctrl+click on help or visit our website.            │
        └──────────────────────────────────────────────────────────────────────┘
    
    Last login: Tue Jul 15 22:34:57 2025
    [oracle@orcl ~]$ ps -ef|grep pmon
    oracle      4420       1  0 22:35 ?        00:00:00 ora_pmon_orcldc
    oracle      5097    5029  0 22:40 pts/0    00:00:00 grep --color=auto pmon
    [oracle@orcl ~]$
    [oracle@orcl ~]$ ps -ef|grep pmon
    oracle      4420       1  0 22:35 ?        00:00:00 ora_pmon_orcldc
    oracle      5129    5029  0 22:50 pts/0    00:00:00 grep --color=auto pmon
    [oracle@orcl ~]$ ps -ef|grep tns
    root           6       2  0 22:34 ?        00:00:00 [netns]
    oracle      2492       1  0 22:35 ?        00:00:00 /u01/app/oracle/product/19.0.0/dbhome_1/bin/tnslsnr LISTENER -inherit
    oracle      5131    5029  0 22:50 pts/0    00:00:00 grep --color=auto tns
    [oracle@orcl ~]$
    [oracle@orcl ~]$ sqlplus / as sysdba
    
    SQL*Plus: Release 19.0.0.0.0 - Production on Tue Jul 15 22:50:43 2025
    Version 19.27.0.0.0
    
    Copyright (c) 1982, 2024, Oracle.  All rights reserved.
    
    
    Connected to:
    Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
    Version 19.27.0.0.0
    
    SQL> show pdbs
    
        CON_ID CON_NAME                       OPEN MODE  RESTRICTED
    ---------- ------------------------------ ---------- ----------
             2 PDB$SEED                       READ ONLY  NO
             3 ORCLPDB                        READ WRITE NO
    SQL>
    SQL> exit
    Disconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
    Version 19.27.0.0.0
    [oracle@orcl ~]$
    [oracle@orcl ~]$
    [oracle@orcl ~]$ lsnrctl status
    
    LSNRCTL for Linux: Version 19.0.0.0.0 - Production on 15-JUL-2025 22:50:53
    
    Copyright (c) 1991, 2025, Oracle.  All rights reserved.
    
    Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=orcl.oraeasy.com)(PORT=1521)))
    STATUS of the LISTENER
    ------------------------
    Alias                     LISTENER
    Version                   TNSLSNR for Linux: Version 19.0.0.0.0 - Production
    Start Date                15-JUL-2025 22:35:06
    Uptime                    0 days 0 hr. 15 min. 48 sec
    Trace Level               off
    Security                  ON: Local OS Authentication
    SNMP                      OFF
    Listener Parameter File   /u01/app/oracle/product/19.0.0/dbhome_1/network/admin/listener.ora
    Listener Log File         /u01/app/oracle/diag/tnslsnr/orcl/listener/alert/log.xml
    Listening Endpoints Summary...
      (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=orcl.oraeasy.com)(PORT=1521)))
      (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
    Services Summary...
    Service "2c3d6bacf692345be0633350a8c050ed" has 1 instance(s).
      Instance "orcldc", status READY, has 1 handler(s) for this service...
    Service "ORCLDC" has 1 instance(s).
      Instance "orcldc", status READY, has 1 handler(s) for this service...
    Service "orcldcXDB" has 1 instance(s).
      Instance "orcldc", status READY, has 1 handler(s) for this service...
    Service "orclpdb" has 1 instance(s).
      Instance "orcldc", status READY, has 1 handler(s) for this service...
    The command completed successfully
    [oracle@orcl ~]$
    [oracle@orcl ~]$
    
    


    Thank you for reading!

    I hope this content has been helpful to you. Your feedback and suggestions are always welcome — feel free to leave a comment or reach out with any queries.

    Abhishek Shrivastava

    📧 Email: oraeasyy@gmail.com
    🌐 Website: www.oraeasy.com
Location: Noida, Uttar Pradesh, India

0 comments:

Post a Comment