oraenv not found error

In my ksh script i set the environmental variables as follows:

#!/bin/ksh
ORAENV_ASK=NO
export ORACLE_SID=ORCL
. oraenv
export ORACLE_HOME

when I run the script manually, it runs fine, but when I run it via crontab, I get this error.

How can I avoid getting this error. Should I try using

. ${HOME}/.profile

or
. /home/oracle/.profile ${ORACLE_SID}

?
LVL 35
YZlatAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DavidSenior Oracle Database AdministratorCommented:
Put your environment and explicit paths into the shell script.
0
YZlatAuthor Commented:
The thing is I want it to be dynamic, that's the whole reason I removed the explicit path that was hardcoded
0
omarfaridCommented:
When you run any script via crontab you need to set for it environment, and when you refer to files or directories then you refer by full path names. You could try the following before sourcing the oraenv file:

cd

OR

cd ~
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

slightwv (䄆 Netminder) Commented:
To further explain:  crontab runs without any environment.  It doesn't source any SHELL profile/rc scripts.

Just set your PATH to include the path to the oraenv script.  or fully qualify it in your crontab script.

For example:  If oraenv is in /etc, in your script:
. /etc/oraenv
0
DavidSenior Oracle Database AdministratorCommented:
My initial suggestion was to add the fully qualified oraenv path.  Also, I've seen one workaround by adding the profile to the cron line: "$HOME/.profile; /u01/scripts/somescript.sh"
0
YZlatAuthor Commented:
I changed . oraenv to . /usr/local/bin/oraenv


and now I am getting an error when trying to export ORACLE_HOME:


/home/oracle/Scripts/MyScript.ksh[16]: dbhome:  not found.



I checked /usr/local/bin and dhome exists there
0
slightwv (䄆 Netminder) Commented:
>>I am getting an error when trying to export ORACLE_HOME

oraenv does this for you.

In your crontab script above the oraenv call:
export PATH=/usr/local/bin:$PATH
0
YZlatAuthor Commented:
/usr/local/bin is already in $PATH

I changed my code to:

ORAENV_ASK=NO
export ORACLE_SID=$1
export PATH=$ORACLE_HOME/bin:$PATH
. /usr/local/bin/oraenv
export ORACLE_HOME

but still getting the same error.

Btw, could you tell me what does ORAENV_ASK=NO do?
0
slightwv (䄆 Netminder) Commented:
>>export PATH=$ORACLE_HOME

This does nothing.  ORACLE_HOME isn't set yet.  Also, oraenv adds ORACLE_HOME to your path for you.

>>/usr/local/bin is already in $PATH

I don't see where you've added it.  Remember, if you are looking from your command prompt, you have executed profile setup scripts (like your .profile and /etc/profile) that does things for you.

When a script is executed from cron, none of these scripts are executed.

>>Btw, could you tell me what does ORAENV_ASK=NO do?

By default oraenv prompts you for the SID.  ORAENV_ASK=NO tells it to take the SID you have already exported and set the environment based on it.
0
YZlatAuthor Commented:
thanks. So how should I modify my code?
0
slightwv (䄆 Netminder) Commented:
Start with this:

ORAENV_ASK=NO
export PATH=/usr/local/bin:$PATH
export ORACLE_SID=$1
. /usr/local/bin/oraenv

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
YZlatAuthor Commented:
It ran, looks like it is working! Thank you!

So . oraenv exports ORACLE_HOME automatically?
0
slightwv (䄆 Netminder) Commented:
>>So . oraenv exports ORACLE_HOME automatically?

Not 100% sure about exporting but it does set it for the rest of the script.  It also adds $ORACLE_HOME/bin to your PATH.

It sets up your ORAcle ENVironment.  Hence the name: oraenv.
0
YZlatAuthor Commented:
one more thing, if I run my script from crontab, I need to set my $PATH again, regardless what it is set to outside of crontab?
0
slightwv (䄆 Netminder) Commented:
>>I need to set my $PATH again, regardless what it is set to outside of crontab?

Again, scripts executed from cron do not execute profile setup scripts.  Anything in your environment while you are logged in as an OS user, doesn't exist when the script is executed from cron.

When you log into Unix, there are profile/rc scripts that get executed.  These scripts set up your environment for that login session.  Cron executed scripts do not execute these.

Anything the script needs as far as environment variables must be set inside the script itself.
0
YZlatAuthor Commented:
Thanks so much for all your help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.