Automatic Startup and Shutdown of Oracle Database Using systemd

Introduction
Understanding systemd
Create a systemd Service Unit File
Add Configuration to the Service Unit File
Edit /etc/oratab
Reload systemd and Enable the Service
Test the Service
Reboot the System
Conclusion

Introduction

In the world of database management, ensuring that your Oracle Database starts and stops reliably is essential for maintaining system availability and data integrity. Fortunately, modern Linux distributions provide robust tools like systemd for managing services, making it easier to automate the startup and shutdown processes.

In this blog post, we'll explore how to use systemd to automatically start and stop an Oracle single-instance database when booting and shutting down your system.

Understanding systemd

Systemd is a system and service manager for Linux operating systems. It's responsible for initializing and managing services during the boot process, and it also handles service control and management, including starting, stopping, and restarting services.

Create a systemd Service Unit File

To automate the startup and shutdown of an Oracle Database, we need to create a systemd service unit file. This file contains instructions for systemd on how to manage the database service.

Open a terminal and create a new service unit file for Oracle Database using your preferred text editor.
For example:

sudo nano /etc/systemd/system/oracle-db.service

Add Configuration to the Service Unit File

Insert the following content into the file

[Unit]
Description=a service to start databases and listener automatically
After=syslog.target network.target

[Service]
LimitNOFILE=1024:65536
LimitNPROC=2047:16384
LimitSTACK=10485760:33554432
LimitMEMLOCK=infinity

Type=forking

User=oracle
Group=oinstall

ExecStart=/opt/oracle/product/21.3.0/dbhome_1/bin/dbstart /opt/oracle/product/21.3.0/dbhome_1
ExecStop=/opt/oracle/product/21.3.0/dbhome_1/bin/dbshut /opt/oracle/product/21.3.0/dbhome_1

RemainAfterExit=True
Restart=no

[Install]
WantedBy=multi-user.target
You'll need to tailor the service unit file to match your Oracle environment. This includes specifying the correct paths to the Oracle Database binaries, startup, and shutdown scripts, as well as setting the appropriate user and group ownership.

Edit /etc/oratab

The following step involves updating the /etc/oratab file to adjust the startup flag for the database

[oracle@dbdocs dbhome_1]$ vi /etc/oratab
dbdocs:/opt/oracle/product/21.3.0/dbhome_1:Y
In the /etc/oratab file, databases marked with a 'Y' as their startup flag will be recognized by dbstart and dbstop commands. This ensures that only databases with the 'Y' flag will be started or stopped automatically.

Reload systemd and Enable the Service

After saving the changes, reload systemd to load the new service unit:

sudo systemctl daemon-reload
Once the service unit file is created and customized, you can enable it using the systemctl enable command. This tells systemd to start the Oracle Database service automatically during system boot.

sudo systemctl enable oracle-db.service

Test the Service

It's crucial to test the systemd setup to ensure that the Oracle Database starts automatically on system boot and shuts down gracefully during system shutdown. You can use commands like systemctl start, systemctl stop, and systemctl status to manage and monitor the service.

sudo systemctl start oracle-db.service

Reboot the System

After testing, reboot your Linux system to ensure that the Oracle Database starts automatically:

sudo reboot

Conclusion

By default, systemd will handle stopping the Oracle Database service during system shutdown. However, if you need custom actions or scripts to be executed before the database shuts down, you can add them to the ExecStop section of the service unit file created above.

With these steps, your Oracle Database should now automatically start and stop with your Linux 8 system. Make sure to test the setup thoroughly to ensure proper functionality.

Related content



Rate Your Experience

: 89 : 1


Last updated in July, 2024

Online Tests
Read more

Oracle Database
Read more

MSSQL Database
Read more

PostGres Database
Read more

Linux
Read more

ASP/C#
Read more

Quick Access