0

I'm currently trying to run my script on startup, being an Oracle 19C script for starting the database and the listener at system boot , I will paste it here (script.sh) . The way I'm trying to work is as manually as possible, I don't want to include it in oratab or anything else. I'm currently working on a Virtual Machine with Oracle Linux 7.9 installed :

#!/bin/bash
# /etc/init.d/script.sh
# chkconfig: 2345 85 60
ORACLE_SID=mysid
ORAENV_ASK=NO
source oraenv

startdatabase(){
sqlplus << EOF
connect / as sysdba
startup
EOF
echo "Database opened"
}

checkingdatabase()
{
 pmon=`ps -ef | grep -w "ora_pmon_$ORACLE_SID"  | grep -v grep`
  if [ "$pmon" != "" ] ; then
    echo "${ORACLE_SID} already opeened."
  else startdatabase
  fi
}
checkingdatabase

listener()
{
 status=`lsnrctl status | grep "Connection refused"`
  if [ "$status" != "" ]; then
  lsnrctl start
        echo "Listener opened"
  else echo "Listener off"
  fi
}
listener

The script is located in /etc/init.d with script.sh being its name. I tried to run it on startup using crontab :

 @reboot sh /etc/init.d/script.sh

or @reboot /etc/init.d/script.sh

I tried also with with article : https://unix.stackexchange.com/questions/188042/running-a-script-during-booting-startup-init-d-vs-cron-reboot but nothing seems to work, also if I do chkconfig --list it will show me this, as it should be running :

script.sh       0:off   1:off   2:on    3:on    4:on    5:on    6:off

The script is also executable, I've tried with rc.local file and also doesn't work :

-rwxrwxr-x.  1 oracle oracle   900 Dec 29 15:16 script.sh

How can I make this work and run at startup ? It's really frustrating, I've tried all the methods and it will not just load at boot and do its job. Any help would be much appreciated ! I've checked also for a lot of articles and none of them help. I want to mention also that the script works just fine when running normally. Thank you !

Bob950
  • 5
  • 3
  • 2
    _" I don't want to include it in oratab or anything else"_ Why not? Everything about starting oracle is built around the assumption that proper entries are in oratab, and referencing it accordingly. Would you also like to replace the tire on your car without using a jack and a lug wrench? Setting scripts to run on startup is well documented. And in your [previous question](https://stackoverflow.com/questions/70500873/how-to-startup-automatically-oracle-19c-using-bash-scripting-and-sql-plus) I showed you how to write a script to start the database. Adding the listener is trivial. – EdStevens Dec 29 '21 at 14:17
  • 1
    Where are you expecting the script to source `oraenv` *from*? – Alex Poole Dec 29 '21 at 14:25
  • 1
    I wouldn't bother with declaring functions for something as straight-forward as this. Especially if you are going to unconditionally call them immediately after they are declared. But if you insist on using them, then you need to actually call them. I don't see where you bothered to call startdatabase. And why are you trying to check if the database is open? If the intent is to run at server startup, then the database will, unconditionally, NOT be open at this point in time. – EdStevens Dec 29 '21 at 14:31

1 Answers1

0
  1. Your script cannot and should not run in a crontab.

Because crontab script has no built in environment. Therefore everything need to be expressed in full/absolute path.

  1. Your script should not be located in /etc/init.d . Because it is not initialized automatically by SysV service nor by systemd service.

  2. Better put your script under user oracle home init folder (assuming oracle is the correct user for running Oracle RDBMS).

    mkdir -p /home/oracle/init
    mv /etc/init.d/script.sh /home/oracle/init
    
  3. Inject user oracle environment variables to your script.

    sed -i '4a source /home/oracle/.bash_profile' /home/oracle/init/script.sh
    
  4. Test running /home/oracle/init/script.sh from another effective user (use sudo or su command).

  5. Reboot.

  6. The correct and better design is to start Oracle RDBMS with a systemd service.

  7. It is possible to start Oracle RDBMS container with docker run ....

Dudi Boy
  • 4,551
  • 1
  • 15
  • 30