Saturday, December 5, 2026

Stop and Start All Oracle Database Homes Using Shell Script and SRVCTL

 

By Shashi Ranjan Singh

Managing multiple Oracle Database Homes on a single server can become challenging, especially when you need to stop and start all database instances without shutting down the Grid Infrastructure services.

In Oracle RAC and standalone environments managed by Oracle Restart or Grid Infrastructure, the srvctl utility provides an efficient way to manage database services and instances.

In this article, we will explore how to automate the shutdown and startup of all Oracle Database Homes configured in /etc/oratab using simple shell scripts and the srvctl stop home and srvctl start home commands.

1. Why Do We Need This Script?

Consider a server hosting multiple Oracle Database versions, with several databases running from different Oracle Homes.

During certain maintenance activities, such as Oracle Database patching or Grid Infrastructure maintenance, you may need to stop all database instances while keeping Grid Infrastructure services running.

One approach is to use:

crsctl stop crs

However, this command stops the entire Clusterware stack, including Grid Infrastructure services.

In some maintenance scenarios, shutting down Grid Infrastructure is unnecessary. We only need to stop the database instances and bring them back online after the maintenance activity.

Oracle provides the following SRVCTL commands for this purpose:

  • srvctl stop home: Stops the resources associated with a specified Oracle Home and saves their state.

  • srvctl start home: Restarts the resources using the previously saved state file.

Instead of executing these commands manually for every Oracle Home, we can automate the process using shell scripts.

2. Environment Details

Let's consider the following example environment.

Hostname: ora-prod-db02

Oracle user: oracle

Grid Infrastructure Home:

/u01/app/19.0.0/grid

Oracle Database Homes:

/u01/app/oracle/product/12.2.0.1/dbhome_1
/u01/app/oracle/product/19.0.0/dbhome_1
/u01/app/oracle/product/19.0.0/dbhome_2
/u01/app/oracle/product/21.0.0/dbhome_1

Databases running on the server:

DatabaseOracle Home
FINCDB19.0.0/dbhome_1
HRDB19.0.0/dbhome_1
SALESCDB19.0.0/dbhome_2
PAYDB12.2.0.1/dbhome_1
REPORTCDB21.0.0/dbhome_1

The objective is to stop all database instances across these Oracle Homes without stopping ASM or Grid Infrastructure services.

3. Understanding the /etc/oratab File

The /etc/oratab file contains the database SID and corresponding Oracle Home information.

Let's look at an example:

[oracle@ora-prod-db02 ~]$ cat /etc/oratab

PAYDB:/u01/app/oracle/product/12.2.0.1/dbhome_1:N
FINCDB:/u01/app/oracle/product/19.0.0/dbhome_1:N
HRDB:/u01/app/oracle/product/19.0.0/dbhome_1:N
SALESCDB:/u01/app/oracle/product/19.0.0/dbhome_2:N
REPORTCDB:/u01/app/oracle/product/21.0.0/dbhome_1:N
+ASM:/u01/app/19.0.0/grid:N

We will use this file as the source to identify all configured Oracle Database Homes.

Notice that multiple databases can share the same Oracle Home. Therefore, we need to remove duplicate Oracle Home paths before executing the SRVCTL commands.

We also need to exclude the ASM entry because our objective is to manage database instances only.

4. Shell Script to Stop All Oracle Database Homes

Create a shell script named stop_all_dbhomes.sh.

#!/bin/bash

ORATAB="/etc/oratab"

# Extract unique Oracle Database Homes
for DBHOME in $(grep -v '^#' "$ORATAB" \
    | grep -v '^+' \
    | cut -d: -f2 \
    | sort -u)
do

    echo "=========================================="
    echo "Stopping Database Home: $DBHOME"
    echo "=========================================="

    if [ -x "$DBHOME/bin/srvctl" ]; then

        # Preserve the previous state file
        if [ -f "$DBHOME/state_file.log" ]; then
            mv "$DBHOME/state_file.log" \
               "$DBHOME/state_file.old"
        fi

        # Stop database resources from this Oracle Home
        "$DBHOME/bin/srvctl" stop home \
            -o "$DBHOME" \
            -s "$DBHOME/state_file.log"

        if [ $? -eq 0 ]; then
            echo "Successfully stopped: $DBHOME"
        else
            echo "ERROR: Failed to stop: $DBHOME"
        fi

    else
        echo "ERROR: SRVCTL not found in $DBHOME"
    fi

done

echo "=========================================="
echo "Database Home shutdown operation completed"
echo "=========================================="

Make the script executable:

chmod +x stop_all_dbhomes.sh

Execute the script using the Oracle software owner:

./stop_all_dbhomes.sh

Expected Output

[oracle@ora-prod-db02 ~]$ ./stop_all_dbhomes.sh

==========================================
Stopping Database Home: /u01/app/oracle/product/12.2.0.1/dbhome_1
==========================================
Successfully stopped: /u01/app/oracle/product/12.2.0.1/dbhome_1

==========================================
Stopping Database Home: /u01/app/oracle/product/19.0.0/dbhome_1
==========================================
Successfully stopped: /u01/app/oracle/product/19.0.0/dbhome_1

==========================================
Stopping Database Home: /u01/app/oracle/product/19.0.0/dbhome_2
==========================================
Successfully stopped: /u01/app/oracle/product/19.0.0/dbhome_2

==========================================
Stopping Database Home: /u01/app/oracle/product/21.0.0/dbhome_1
==========================================
Successfully stopped: /u01/app/oracle/product/21.0.0/dbhome_1

==========================================
Database Home shutdown operation completed
==========================================

5. Verify That All Database Instances Are Stopped

After executing the shutdown script, verify the database background processes.

ps -ef | grep pmon

Expected output:

grid      2451      1  0 10:15 ?  00:00:00 asm_pmon_+ASM
oracle   28140      1  0 10:20 ?  00:00:00 grep pmon

As shown above, the ASM instance is still running, while the database PMON processes are no longer present.

This confirms that database instances have been stopped without shutting down the ASM instance.

6. Shell Script to Start All Oracle Database Homes

Once the maintenance activity is completed, we can start the database instances using the state files generated during the shutdown operation.

Create another shell script named start_all_dbhomes.sh.

#!/bin/bash

ORATAB="/etc/oratab"

# Extract unique Oracle Database Homes
for DBHOME in $(grep -v '^#' "$ORATAB" \
    | grep -v '^+' \
    | cut -d: -f2 \
    | sort -u)
do

    echo "=========================================="
    echo "Starting Database Home: $DBHOME"
    echo "=========================================="

    if [ -x "$DBHOME/bin/srvctl" ]; then

        if [ -f "$DBHOME/state_file.log" ]; then

            # Start database resources using saved state
            "$DBHOME/bin/srvctl" start home \
                -o "$DBHOME" \
                -s "$DBHOME/state_file.log"

            if [ $? -eq 0 ]; then
                echo "Successfully started: $DBHOME"
            else
                echo "ERROR: Failed to start: $DBHOME"
            fi

        else
            echo "WARNING: State file not found for $DBHOME"
        fi

    else
        echo "ERROR: SRVCTL not found in $DBHOME"
    fi

done

echo "=========================================="
echo "Database Home startup operation completed"
echo "=========================================="

Make the script executable:

chmod +x start_all_dbhomes.sh

Execute the script:

./start_all_dbhomes.sh

Expected Output

[oracle@ora-prod-db02 ~]$ ./start_all_dbhomes.sh

==========================================
Starting Database Home: /u01/app/oracle/product/12.2.0.1/dbhome_1
==========================================
Successfully started: /u01/app/oracle/product/12.2.0.1/dbhome_1

==========================================
Starting Database Home: /u01/app/oracle/product/19.0.0/dbhome_1
==========================================
Successfully started: /u01/app/oracle/product/19.0.0/dbhome_1

==========================================
Starting Database Home: /u01/app/oracle/product/19.0.0/dbhome_2
==========================================
Successfully started: /u01/app/oracle/product/19.0.0/dbhome_2

==========================================
Starting Database Home: /u01/app/oracle/product/21.0.0/dbhome_1
==========================================
Successfully started: /u01/app/oracle/product/21.0.0/dbhome_1

==========================================
Database Home startup operation completed
==========================================

7. Verify That All Database Instances Are Online

After executing the startup script, check the PMON processes again.

ps -ef | grep pmon

Expected output:

grid      2451      1  0 10:15 ?  00:00:00 asm_pmon_+ASM
oracle   30210      1  0 10:45 ?  00:00:00 ora_pmon_PAYDB
oracle   30215      1  0 10:45 ?  00:00:00 ora_pmon_FINCDB
oracle   30220      1  0 10:45 ?  00:00:00 ora_pmon_HRDB
oracle   30225      1  0 10:45 ?  00:00:00 ora_pmon_SALESCDB
oracle   30230      1  0 10:45 ?  00:00:00 ora_pmon_REPORTCDB

All database instances are online again, and the ASM instance has remained running throughout the operation.

8. Understanding the Script Logic

Let's understand the commands used in both scripts.

Step 1: Remove commented lines

grep -v '^#' /etc/oratab

This excludes lines starting with # from the /etc/oratab file.

Step 2: Exclude ASM entries

grep -v '^+'

ASM entries generally start with +, such as +ASM. We exclude these entries because we only want Oracle Database Homes.

Step 3: Extract Oracle Home paths

cut -d: -f2

The colon (:) is used as the field delimiter, and -f2 extracts the second field containing the Oracle Home path.

Step 4: Sort and remove duplicate Oracle Homes

sort -u

This sorts the Oracle Home paths and removes duplicates.

For example, if three databases are configured under the same Oracle Home, that home will appear only once in the output.

Step 5: Execute SRVCTL commands

For each unique Oracle Home, the script executes:

$DBHOME/bin/srvctl stop home \
    -o $DBHOME \
    -s $DBHOME/state_file.log

The -o parameter specifies the Oracle Home, while -s specifies the state file used to save the resource state.

During startup, the same state file is supplied to the corresponding srvctl start home command.

9. Important Considerations

Before implementing these scripts in a production environment, consider the following points:

  1. Run the scripts using the appropriate Oracle software owner and ensure that the user has the required permissions.

  2. Verify that the /etc/oratab file contains the correct Oracle Home paths.

  3. Ensure that the respective Oracle Home contains a compatible srvctl utility.

  4. Keep the state files safe until the startup operation is completed.

  5. Review the SRVCTL output and verify the actual resource status rather than relying only on the script's exit code.

  6. Test the scripts in a non-production environment before using them during planned maintenance.

Important: srvctl stop home and srvctl start home behavior and supported resource types can vary across Oracle versions and configurations. Validate the commands against the installed Oracle version and Oracle Restart or RAC environment. These scripts are not a replacement for a complete Clusterware shutdown or startup procedure.

10. Conclusion

When managing multiple Oracle Database Homes on the same server, manually stopping and starting individual database instances can be time-consuming and error-prone.

By combining the /etc/oratab file with shell scripting and SRVCTL, we can automate the shutdown and startup of database resources across multiple Oracle Homes.

The biggest advantage of this approach is that we can stop and restart database instances while keeping Grid Infrastructure services, including ASM, running.

This can be particularly useful during database maintenance and patching activities where shutting down the complete Grid Infrastructure stack is not required.

Happy Learning!

— Shashi Ranjan Singh

No comments:

Post a Comment