Systemd script to shutdown the DB gracefully during linux server reboot

I am trying to write a systemd script that will gracefully shutdown the oracle DB during server reboot. The script i have created in /etc/systemd/system as follows. The script /usr/local/bin/oracle_db_shutdown.sh will do shit immediate the database and it is working fine. But i think issue is with this systemd service script. If i reboot the server it is not gracefully doing shut immediate but terminating the database. Can someone help pls

[Unit]
Description=Shutdown Oracle database before reboot
DefaultDependencies=no
Before=shutdown.target reboot.target halt.target
Conflicts=shutdown.target
Requires=network.target

[Service]
Type=oneshot
ExecStart=/usr/local/bin/oracle_db_shutdown.sh
RemainAfterExit=true
TimeoutStopSec=300 # Give enough time for the DB to shut down gracefully
KillMode=none

[Install]
WantedBy=halt.target reboot.target shutdown.target

Hi @sagnikcse24,

it is usually not necessary to write an extra service unit for this purpose. A service unit usually contains an ExecStop= and/or KillSignal= directive. These will stop the services cleanly before rebooting. If your DB won't be stopped cleanly, please post the corresponding log entries and the oracle-db service unit.

My script to shutdown immediate the oracle DB is "oracle_db_shutdown.sh" which is mentioned below.

[root@aws1-ldb-d65 system]# cat /usr/local/bin/oracle_db_shutdown.sh
#!/bin/bash
# This script will be called during system shutdown/reboot to stop the Oracle database

ORACLE_HOME=/oradb/oracle/app/product/19.0.0/dbhome_1
ORACLE_USER=oracle
ORACLE_SID=PATCHCDB
LOGFILE=/var/log/oracle_db_shutdown.log

echo "Shutting down the Oracle database..."

# Switch to the Oracle user and shut down the database
sudo -u oracle -i <<EOF >>$LOGFILE 2>&1
export ORACLE_HOME=$ORACLE_HOME
export ORACLE_SID=$ORACLE_SID
$ORACLE_HOME/bin/sqlplus / as sysdba <<SQL
shutdown immediate;
exit;
SQL
EOF

if [ $? -eq 0 ]; then
  echo "[$(date)] Oracle database shut down complete." | tee -a $LOGFILE
else
  echo "[$(date)] Failed to shut down the Oracle database." | tee -a $LOGFILE
fi

Then the service file created from root in /etc/systemd/system/oracle_db_shutdown.service, the content of which is as follows.

[root@aws1-ldb-d65 system]# cat oracle_db_shutdown.service
[Unit]
Description=Shutdown Oracle database before reboot
DefaultDependencies=no
Before=shutdown.target reboot.target halt.target
Conflicts=shutdown.target
Requires=network.target

[Service]
Type=oneshot
ExecStart=/usr/local/bin/oracle_db_shutdown.sh
RemainAfterExit=yes
KillMode=none
TimeoutStopSec=2min
User=oracle
Group=dba

[Install]
WantedBy=halt.target reboot.target shutdown.target

When i am issuing reboot the alert log just says Database is terminated without gracefully shut immeDIATE AS I AM TRYING TO DO. So it is not working at all.

I am getting this in the log file where i am writing the shutdown immediate result

[root@aws1-ldb-d65 system]# journalctl -flu oracle_db_shutdown.service
-- Logs begin at Wed 2024-07-24 10:58:47 EDT. --
Jul 24 11:07:56 aws1-ldb-d65 systemd[1]: Starting Shutdown Oracle database before reboot...
Jul 24 11:07:56 aws1-ldb-d65 oracle_db_shutdown.sh[10895]: Shutting down the Oracle database...
Jul 24 11:07:56 aws1-ldb-d65 oracle_db_shutdown.sh[10895]: /usr/local/bin/oracle_db_shutdown.sh: line 12: /var/log/oracle_db_shutdown.log: Permission denied
Jul 24 11:07:56 aws1-ldb-d65 oracle_db_shutdown.sh[10895]: tee: /var/log/oracle_db_shutdown.log: Permission denied
Jul 24 11:07:56 aws1-ldb-d65 systemd[1]: oracle_db_shutdown.service: main process exited, code=exited, status=1/FAILURE
Jul 24 11:07:56 aws1-ldb-d65 systemd[1]: Failed to start Shutdown Oracle database before reboot.
Jul 24 11:07:56 aws1-ldb-d65 systemd[1]: Unit oracle_db_shutdown.service entered failed state.
Jul 24 11:07:56 aws1-ldb-d65 systemd[1]: oracle_db_shutdown.service failed.

Both the >>$LOGFILE and the | tee -a $LOGFILE run as root.
Perhaps /var/log/oracle_db_shutdown.log is on a network share?
Check with

df /var/log/oracle_db_shutdown.log

That is solved..Editted the shutdown script:

[root@aws1-ldb-d65 system]# cat /usr/local/bin/oracle_db_shutdown.sh
#!/bin/bash
# This script will be called during system shutdown/reboot to stop the Oracle database

ORACLE_HOME=/oradb/oracle/app/product/19.0.0/dbhome_1
ORACLE_USER=oracle
ORACLE_SID=PATCHCDB
LOGFILE=/home/oracle/scripts/oracle_db_shutdown.log

echo "Shutting down the Oracle database..."

# Switch to the Oracle user and shut down the database
sudo -u oracle -i <<EOF >>$LOGFILE 2>&1
export ORACLE_HOME=$ORACLE_HOME
export ORACLE_SID=$ORACLE_SID
$ORACLE_HOME/bin/sqlplus / as sysdba <<SQL
shutdown immediate;
exit;
SQL
EOF

if [ $? -eq 0 ]; then
  echo "[$(date)] Oracle database shut down complete." | tee -a $LOGFILE
else
  echo "[$(date)] Failed to shut down the Oracle database." | tee -a $LOGFILE
fi

But now while after reboot getting this

[root@aws1-ldb-d65 system]# systemctl status oracle_db_shutdown.service
● oracle_db_shutdown.service - Shutdown Oracle database before reboot
   Loaded: loaded (/etc/systemd/system/oracle_db_shutdown.service; enabled; vendor preset: disabled)
   Active: inactive (dead)

If i do a check with systemctl start oracle_db_shutdown.service it seems to be fine. Then why if i reboot the server DB is not going down gracefully

[root@aws1-ldb-d65 system]# systemctl start oracle_db_shutdown.service
[root@aws1-ldb-d65 system]# systemctl status oracle_db_shutdown.service
● oracle_db_shutdown.service - Shutdown Oracle database before reboot
Loaded: loaded (/etc/systemd/system/oracle_db_shutdown.service; enabled; vendor preset: disabled)
Active: active (exited) since Wed 2024-07-24 13:00:48 EDT; 13s ago
Process: 26005 ExecStart=/usr/local/bin/oracle_db_shutdown.sh (code=exited, status=0/SUCCESS)
Main PID: 26005 (code=exited, status=0/SUCCESS)

Jul 24 12:57:51 aws1-ldb-d65 systemd[1]: Starting Shutdown Oracle database before reboot...
Jul 24 12:57:51 aws1-ldb-d65 oracle_db_shutdown.sh[26005]: Shutting down the Oracle database...
Jul 24 12:57:51 aws1-ldb-d65 sudo[26006]: oracle : TTY=unknown ; PWD=/ ; USER=oracle ; COMMAND=/bin/bash
Jul 24 13:00:48 aws1-ldb-d65 systemd[1]: Started Shutdown Oracle database before reboot.
[root@aws1-ldb-d65 system]#

I think, and was suggested already, for shutdown it must be
ExecStop=/usr/local/bin/oracle_db_shutdown.sh
And perhaps a
ExecStart=/bin/true
to make systemd happy during booting.

1x1 Latest update :

Now this systemd service file "oracle_db_shutdown.service" is working and the logs are written to the log file. Have changed from ExecStart to ExecStop and writing to a logfile to verify whether it is calling or not.

[Unit]
Description=Shutdown Oracle database before reboot
DefaultDependencies=no
Before=shutdown.target reboot.target halt.target
Conflicts=shutdown.target
Requires=network.target

[Service]
Type=oneshot
ExecStop=/usr/local/bin/log_oracle_shutdown.sh
RemainAfterExit=true
KillMode=none
TimeoutStopSec=300
User=oracle
Group=oinstall

[Install]
WantedBy=halt.target reboot.target shutdown.target

=================================

But when i see the log it is as follows : That means gracefull shutdown is not happening. What changes can be done in the systemd service file "oracle_db_shutdown.service" to have the shutdown immediate working during "reboot" command.

SQL> ORA-01092: ORACLE instance terminated. Disconnection forced
SQL> Disconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.20.0.0.0
[Wed Jul 24 14:18:37 EDT 2024] Oracle database shut down complete.
[oracle@aws1-ldb-d65 scripts]$

Please suggest