Home » Infrastructure » Linux » Shell Script Doesn't Set Variables Correctly When Run From Cron (RHEL 5 + Oracle 10.2.0.5/11.1.0.7/11.2.0)
Shell Script Doesn't Set Variables Correctly When Run From Cron [message #611115] Thu, 27 March 2014 13:33 Go to next message
coreyr
Messages: 18
Registered: March 2014
Junior Member
I've written a basic script that does a datapump export of all databases running on the server, zips/moves them to a final destination, and writes a success/failure message for each to a status table.

The script runs fine when I kick it off from a command line but fails when I kick it off from cron -- and it appears to be because ORAENV isn't being executed (and/or some other variables or paths aren't being set correctly.)

All of the searching I've done hasn't given me enough of a clue to be able to resolve things so I'm hoping somebody can help out.

Shown below are: 1) The cron entry; 2) The script itself; 3) Output from a successful command-line execution; and 4) Output from a failed cron execution.

Thanks for your consideration and let me know if I can provide additional details.

Corey

________________________________________________________________________________________________________________________


Cron entry:
# Datapump export of all running databases
##########################################
40 12 * * 1-5 /opt/oracle/scripts/datapump_export_cron.sh >/opt/oracle/scripts/logs/datapump_cron.txt


Script:
#!/bin/bash -xv


# Define the log file that will store all datapump processing info
##################################################################
  LOG=/opt/oracle/scripts/logs/datapump_exports.log
  DATETIME=`date +%c`
  echo "----------- ${DATETIME} ------------------"  >> ${LOG}
  echo -e "\n" >> ${LOG}


# Loop through each of the databases for which an export is to be done, setting the appropriate variables
#########################################################################################################
  for INSTANCE in klwtst lvdfcbdv kcorptst
# for INSTANCE in `ps -ef | grep pmon | grep -v grep | awk 'BEGIN{FS="_"}{print $NF}'`
  do
    echo "The Oracle SID is this:  $ORACLE_SID" >> ${LOG}
    echo "Oracle Home is this:  $ORACLE_HOME" >> ${LOG}
    echo "The path is: $PATH" >> ${LOG}
    export ORACLE_SID=${INSTANCE}
    export ORAENV_ASK=NO;
    . oraenv;
    echo "The Oracle SID is this:  $ORACLE_SID" >> ${LOG}
    echo "Oracle Home is this:  $ORACLE_HOME" >> ${LOG}
    echo "The path is: $PATH" >> ${LOG}
    VERSION=`echo $ORACLE_HOME | awk 'BEGIN{FS="/"}{print $NF}' | cut -c 1-2`
      if [ ${VERSION} == 10 ]
      then
           COMPRESSION_TYPE="metadata_only"
      else
           COMPRESSION_TYPE="all"
      fi
    PWD1=`head -1 /opt/oracle/scripts/.pwd.txt`
    PWD2=`tail -1 /opt/oracle/scripts/.pwd.txt`
    DATE=`date +%Y%m%d`
    DUMPDIR=/opt/oracle/scripts/datapump_exports/datapump_temp
    MOVEDIR=/opt/oracle/scripts/datapump_exports/${ORACLE_SID}
    LOGDIR=/opt/oracle/scripts/logs/${ORACLE_SID}
    TEMPLOG=/opt/oracle/scripts/logs/.datapump_temp.log
    UPPER_SID=`echo $ORACLE_SID | tr '[:lower:]' '[:upper:]'`
    SERVER=`uname -n | awk -F. '{print $1}' | tr '[:lower:]' '[:upper:]'`


  # Export the database's objects via datapump
  ############################################
    echo "Exporting objects for ${ORACLE_SID} now.........................." >> ${LOG}
    expdp system/${PWD1} parallel=4 directory=DP_EXPORTS dumpfile=dpexport%u_${ORACLE_SID}_${DATE}.dmp \
    logfile=dpexport_${DATE}.log job_name=${ORACLE_SID} full=y exclude=statistics compression=${COMPRESSION_TYPE} \
    flashback_time=\"to_timestamp\(to_char\(sysdate,\'yyyy-mm-dd hh24:mi:ss\'\),\'yyyy-mm-dd hh24:mi:ss\'\)\"
    tail -1 ${DUMPDIR}/dpexport_${DATE}.log >> ${LOG}
    echo -e "\n" >> ${LOG}
    tail -1 ${DUMPDIR}/dpexport_${DATE}.log >> ${TEMPLOG}
    DP_LAST_TIME=`cat ${TEMPLOG} | tail -2 | head -1 | awk 'BEGIN{FS=" "}{print $6}'`
    DP_THIS_TIME=`cat ${TEMPLOG}| tail -1 | awk 'BEGIN{FS=" "}{print $6}'`
    cat ${TEMPLOG} | tail -1 | grep -qw 'successfully'
    if [ $? -eq 0 ] && [ "$DP_LAST_TIME" != $"DP_THIS_TIME" ]
    then
      DP_STATUS=succeeded
    else
      DP_STATUS=failed
    fi


  # Move the export and log files to their final destinations and tar/gzip them
  #############################################################################
    if [ ! -d "$MOVEDIR" ]
    then
      mkdir /opt/oracle/scripts/datapump_exports/${ORACLE_SID}
      mkdir /opt/oracle/scripts/logs/${ORACLE_SID}
    fi
    mv ${DUMPDIR}/*.dmp ${MOVEDIR}
    cd ${MOVEDIR}
    tar -czvf dp_${ORACLE_SID}_${DATE}.tar.gz *.dmp
    rm ${MOVEDIR}/*.dmp
    mv ${DUMPDIR}/dpexport_${DATE}.log ${LOGDIR}/dp_${ORACLE_SID}_${DATE}.log
    gzip ${LOGDIR}/dp_${ORACLE_SID}_${DATE}.log


  # Remove export and log files older than three days
  ###################################################
    find ${MOVEDIR} -mtime +3 -type f -name "*.gz" -exec rm -f {} \;
    find ${LOGDIR} -mtime +3 -type f -name "*.gz" -exec rm -f {} \;


  # Write status of export to status table
  ########################################
    export PWD2
    export DP_STATUS
    export DATE
    export UPPER_SID
    export SERVER
    sqlplus -S /nolog <<-EOF
    connect dp_user/$PWD2@rman2
    declare
      db_id NUMBER;
      max_db_id NUMBER;
    begin
      select id into db_id from dp_user.dp_status where database_name='$UPPER_SID';
      if db_id is not null
      then
        update dp_user.dp_status set datapump_status='$DP_STATUS', last_run_date='$DATE'
        where database_name='$UPPER_SID';
      end if;
      exception
        when no_data_found then
        select max(id) into max_db_id from dp_user.dp_status;
        insert into dp_user.dp_status values(max_db_id+1,'$UPPER_SID','$SERVER','$DP_STATUS','$DATE');
    end;
    /
    exit
EOF
  done


Success (command-line):

----------- Thu 27 Mar 2014 08:07:46 AM CDT ------------------

The Oracle SID is this:
Oracle Home is this:  /opt/oracle/product/11.1.0.7
The path is: /usr/kerberos/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/bin:/usr/bin:/home/oracle/bin:/opt/oracle/product/11.1.0.7/bin
The Oracle SID is this:  klwtst
Oracle Home is this:  /opt/oracle/product/11.1.0.7
The path is: /usr/kerberos/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/bin:/usr/bin:/home/oracle/bin:/opt/oracle/product/11.1.0.7/bin
Exporting objects for klwtst now..........................
Job "SYSTEM"."KLWTST" successfully completed at 08:11:21


The Oracle SID is this:  klwtst
Oracle Home is this:  /opt/oracle/product/11.1.0.7
The path is: /usr/kerberos/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/bin:/usr/bin:/home/oracle/bin:/opt/oracle/product/11.1.0.7/bin
The Oracle SID is this:  lvdfcbdv
Oracle Home is this:  /opt/oracle/product/11.2.0
The path is: /usr/kerberos/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/bin:/usr/bin:/home/oracle/bin:/opt/oracle/product/11.2.0/bin
Exporting objects for lvdfcbdv now..........................
Job "SYSTEM"."LVDFCBDV" successfully completed at 08:15:24


The Oracle SID is this:  lvdfcbdv
Oracle Home is this:  /opt/oracle/product/11.2.0
The path is: /usr/kerberos/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/bin:/usr/bin:/home/oracle/bin:/opt/oracle/product/11.2.0/bin
The Oracle SID is this:  kcorptst
Oracle Home is this:  /opt/oracle/product/10.2.0.5
The path is: /usr/kerberos/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/bin:/usr/bin:/home/oracle/bin:/opt/oracle/product/10.2.0.5/bin
Exporting objects for kcorptst now..........................
Job "SYSTEM"."KCORPTST" successfully completed at 08:17:42



Failure (cron):

----------- Thu Mar 27 09:12:01 2014 ------------------

The Oracle SID is this:
Oracle Home is this:
The path is: /usr/bin:/bin
The Oracle SID is this:  klwtst
Oracle Home is this:
The path is: /usr/bin:/bin
Exporting objects for klwtst now..........................


The Oracle SID is this:  klwtst
Oracle Home is this:
The path is: /usr/bin:/bin
The Oracle SID is this:  kcorptst
Oracle Home is this:
The path is: /usr/bin:/bin


The Oracle SID is this:  kcorptst
Oracle Home is this:
The path is: /usr/bin:/bin
The Oracle SID is this:  lvdfcbdv
Oracle Home is this:
The path is: /usr/bin:/bin

Re: Shell Script Doesn't Set Variables Correctly When Run From Cron [message #611116 is a reply to message #611115] Thu, 27 March 2014 13:49 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
To better understand reality add the line below as the second line of /opt/oracle/scripts/datapump_export_cron.sh
env | sort -o /tmp/expdp_capture.env

after cron invokes the script, inspect the content of /tmp/expdp_capture.env

you just have to ensure a few key environmental variables are established before the real code starts to execute
ORACLE_SID
ORACLE_HOME
PATH
Re: Shell Script Doesn't Set Variables Correctly When Run From Cron [message #611117 is a reply to message #611115] Thu, 27 March 2014 13:55 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

.profile/.bash_profile/.bash_rc... are not sourced when execution comes from cron, you have to explicitly do it in your script.

Re: Shell Script Doesn't Set Variables Correctly When Run From Cron [message #611118 is a reply to message #611116] Thu, 27 March 2014 13:59 Go to previous messageGo to next message
coreyr
Messages: 18
Registered: March 2014
Junior Member
Here are the contents of the file:
oracle /tmp> cat expdp_capture.env
HOME=/home/oracle
LOGNAME=oracle
PATH=/usr/bin:/bin
PWD=/home/oracle
SHELL=/bin/sh
SHLVL=2
USER=oracle
_=/usr/bin/env

I would have figured my profile would have automatically been put into place, but obviously not.

So if I source it (at the top of the script), will that cause ORAENV to be executed?

Re: Shell Script Doesn't Set Variables Correctly When Run From Cron [message #611119 is a reply to message #611118] Thu, 27 March 2014 14:17 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

You will have the same behaviour than at prompt.

Re: Shell Script Doesn't Set Variables Correctly When Run From Cron [message #611120 is a reply to message #611118] Thu, 27 March 2014 14:18 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>So if I source it (at the top of the script), will that cause ORAENV to be executed?
actual testing provides better & faster answers.
It does not work now so it is not possible to make it worse.

[Updated on: Thu, 27 March 2014 14:18]

Report message to a moderator

Re: Shell Script Doesn't Set Variables Correctly When Run From Cron [message #611122 is a reply to message #611120] Thu, 27 March 2014 15:25 Go to previous messageGo to next message
coreyr
Messages: 18
Registered: March 2014
Junior Member
I sourced the profile (by adding . $HOME/.bash_profile to the second line), set the job to run in cron, and the result was that the processing of the first database in the loop worked exactly as expected.

It appears that for the other databases, though, the proper variables (ORACLE_SID, ORACLE_HOME, and PATH) were set correctly but the script skipped all of the actual processing (see below).

My first thought was that maybe other variables need to be set (e.g., added to PATH) while inside the loop correctly, but I'm not sure.

[code]
----------- Thu 27 Mar 2014 02:56:01 PM CDT ------------------


The Oracle SID is this:
Oracle Home is this: /opt/oracle/product/11.1.0.7
The path is: /usr/kerberos/bin:/usr/bin:/bin:/home/oracle/bin:/opt/oracle/product/11.1.0.7/bin
The instance is: klwtst
The Oracle SID is this: klwtst
Oracle Home is this: /opt/oracle/product/11.1.0.7
The path is: /usr/kerberos/bin:/usr/bin:/bin:/home/oracle/bin:/opt/oracle/product/11.1.0.7/bin
The instance is: klwtst
Exporting objects for klwtst now..........................
Job "SYSTEM"."KLWTST" successfully completed at 14:59:31


The Oracle SID is this: klwtst
Oracle Home is this: /opt/oracle/product/11.1.0.7
The path is: /usr/kerberos/bin:/usr/bin:/bin:/home/oracle/bin:/opt/oracle/product/11.1.0.7/bin
The instance is: kcorptst
The Oracle SID is this: kcorptst
Oracle Home is this: /opt/oracle/product/10.2.0.5
The path is: /usr/kerberos/bin:/usr/bin:/bin:/home/oracle/bin:/opt/oracle/product/10.2.0.5/bin
The instance is: kcorptst
Exporting objects for kcorptst now..........................


The Oracle SID is this: kcorptst
Oracle Home is this: /opt/oracle/product/10.2.0.5
The path is: /usr/kerberos/bin:/usr/bin:/bin:/home/oracle/bin:/opt/oracle/product/10.2.0.5/bin
The instance is: kimtst
The Oracle SID is this: kimtst
Oracle Home is this: /opt/oracle/product/11.1.0.7
The path is: /usr/kerberos/bin:/usr/bin:/bin:/home/oracle/bin:/opt/oracle/product/11.1.0.7/bin
The instance is: kimtst
Exporting objects for kimtst now..........................


The Oracle SID is this: kimtst
Oracle Home is this: /opt/oracle/product/11.1.0.7
The path is: /usr/kerberos/bin:/usr/bin:/bin:/home/oracle/bin:/opt/oracle/product/11.1.0.7/bin
The instance is: kottst
The Oracle SID is this: kottst
Oracle Home is this: /opt/oracle/product/11.1.0.7
The path is: /usr/kerberos/bin:/usr/bin:/bin:/home/oracle/bin:/opt/oracle/product/11.1.0.7/bin
The instance is: kottst
Exporting objects for kottst now..........................


The Oracle SID is this: kottst
Oracle Home is this: /opt/oracle/product/11.1.0.7
The path is: /usr/kerberos/bin:/usr/bin:/bin:/home/oracle/bin:/opt/oracle/product/11.1.0.7/bin
The instance is: kumtst
The Oracle SID is this: kumtst
Oracle Home is this: /opt/oracle/product/11.1.0.7
The path is: /usr/kerberos/bin:/usr/bin:/bin:/home/oracle/bin:/opt/oracle/product/11.1.0.7/bin
The instance is: kumtst
Exporting objects for kumtst now..........................


The Oracle SID is this: kumtst
Oracle Home is this: /opt/oracle/product/11.1.0.7
The path is: /usr/kerberos/bin:/usr/bin:/bin:/home/oracle/bin:/opt/oracle/product/11.1.0.7/bin
The instance is: kfcbpro7
The Oracle SID is this: kfcbpro7
Oracle Home is this: /opt/oracle/product/10.2.0.5
The path is: /usr/kerberos/bin:/usr/bin:/bin:/home/oracle/bin:/opt/oracle/product/10.2.0.5/bin
The instance is: kfcbpro7
Exporting objects for kfcbpro7 now..........................


The Oracle SID is this: kfcbpro7
Oracle Home is this: /opt/oracle/product/10.2.0.5
The path is: /usr/kerberos/bin:/usr/bin:/bin:/home/oracle/bin:/opt/oracle/product/10.2.0.5/bin
The instance is: kbpntst
The Oracle SID is this: kbpntst
Oracle Home is this: /opt/oracle/product/11.1.0.7
The path is: /usr/kerberos/bin:/usr/bin:/bin:/home/oracle/bin:/opt/oracle/product/11.1.0.7/bin
The instance is: kbpntst
Exporting objects for kbpntst now..........................


The Oracle SID is this: kbpntst
Oracle Home is this: /opt/oracle/product/11.1.0.7
The path is: /usr/kerberos/bin:/usr/bin:/bin:/home/oracle/bin:/opt/oracle/product/11.1.0.7/bin
The instance is: lvouat
The Oracle SID is this: lvouat
Oracle Home is this: /opt/oracle/product/11.2.0
The path is: /usr/kerberos/bin:/usr/bin:/bin:/home/oracle/bin:/opt/oracle/product/11.2.0/bin
The instance is: lvouat
Exporting objects for lvouat now..........................


The Oracle SID is this: lvouat
Oracle Home is this: /opt/oracle/product/11.2.0
The path is: /usr/kerberos/bin:/usr/bin:/bin:/home/oracle/bin:/opt/oracle/product/11.2.0/bin
The instance is: lvdfcbdv
The Oracle SID is this: lvdfcbdv
Oracle Home is this: /opt/oracle/product/11.2.0
The path is: /usr/kerberos/bin:/usr/bin:/bin:/home/oracle/bin:/opt/oracle/product/11.2.0/bin
The instance is: lvdfcbdv
Exporting objects for lvdfcbdv now..........................


The Oracle SID is this: lvdfcbdv
Oracle Home is this: /opt/oracle/product/11.2.0
The path is: /usr/kerberos/bin:/usr/bin:/bin:/home/oracle/bin:/opt/oracle/product/11.2.0/bin
The instance is: lvdfcbqa
The Oracle SID is this: lvdfcbqa
Oracle Home is this: /opt/oracle/product/11.2.0
The path is: /usr/kerberos/bin:/usr/bin:/bin:/home/oracle/bin:/opt/oracle/product/11.2.0/bin
The instance is: lvdfcbqa
Exporting objects for lvdfcbqa now..........................
Re: Shell Script Doesn't Set Variables Correctly When Run From Cron [message #611123 is a reply to message #611122] Thu, 27 March 2014 17:16 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
add the line below as 2nd or 3rd line in the script invoked by cron
set -x
this "logs" every command executed by the shell; which should provide clues as to what is & is not actually being executed
Re: Shell Script Doesn't Set Variables Correctly When Run From Cron [message #611153 is a reply to message #611123] Fri, 28 March 2014 08:28 Go to previous messageGo to next message
coreyr
Messages: 18
Registered: March 2014
Junior Member
Thanks for all the help so far.

I added the line as you suggested, so now the first four lines of the script look like this:
#!/bin/bash
. $HOME/.bash_profile
env | sort -o /tmp/expdp_capture.env
set -x


Not sure where the output to "set -x" is directed, but the entry in $LOG is as follows:
----------- Fri 28 Mar 2014 08:00:01 AM CDT ------------------

The Oracle SID is this:
Oracle Home is this:  /opt/oracle/product/11.1.0.7
The path is: /usr/kerberos/bin:/usr/bin:/bin:/home/oracle/bin:/opt/oracle/product/11.1.0.7/bin
The instance is: klwtst  
## nothing else is logged beyond the line above

Re: Shell Script Doesn't Set Variables Correctly When Run From Cron [message #611154 is a reply to message #611153] Fri, 28 March 2014 08:32 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
results should be in /opt/oracle/scripts/logs/datapump_cron.txt
Re: Shell Script Doesn't Set Variables Correctly When Run From Cron [message #611158 is a reply to message #611154] Fri, 28 March 2014 08:49 Go to previous messageGo to next message
coreyr
Messages: 18
Registered: March 2014
Junior Member
That's what I thought, but that file is empty:
-rw-rw-r-- 1 oracle oinstall       0 Mar 28 08:00 datapump_cron.txt
Re: Shell Script Doesn't Set Variables Correctly When Run From Cron [message #611163 is a reply to message #611158] Fri, 28 March 2014 09:06 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
start debugging this mystery
Re: Shell Script Doesn't Set Variables Correctly When Run From Cron [message #611166 is a reply to message #611163] Fri, 28 March 2014 09:21 Go to previous messageGo to next message
coreyr
Messages: 18
Registered: March 2014
Junior Member
Going down that road now Smile
Re: Shell Script Doesn't Set Variables Correctly When Run From Cron [message #611167 is a reply to message #611166] Fri, 28 March 2014 10:32 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
first make it work; then make it fancy
[oracle@localhost ~]$ cat test.sh
set -x
echo $ORACLE_SID
touch $ORACLE_SID
ls -l $ORACLE_SID
exit
[oracle@localhost ~]$ chmod 775 test.sh
[oracle@localhost ~]$ ./test.sh > test.out
++ echo orcl
++ touch orcl
++ ls -l orcl
++ exit
[oracle@localhost ~]$ cat test.out
orcl
-rw-rw-r-- 1 oracle oracle 0 Mar 28 08:20 orcl
[oracle@localhost ~]$ 

Re: Shell Script Doesn't Set Variables Correctly When Run From Cron [message #611249 is a reply to message #611167] Mon, 31 March 2014 07:35 Go to previous messageGo to next message
coreyr
Messages: 18
Registered: March 2014
Junior Member
Gotcha. I will try running this (or something very similar) and post the results.

Thanks.
Re: Shell Script Doesn't Set Variables Correctly When Run From Cron [message #611250 is a reply to message #611249] Mon, 31 March 2014 08:13 Go to previous messageGo to next message
coreyr
Messages: 18
Registered: March 2014
Junior Member
Created a script (datapump_test.sh) that ran from the command line and printed (I think) the expected output to the screen; ran the same script from cron which created an empty log file.

Shown below: 1) The script; 2) Command-line output; 3) Cron entry; 4) Cron log file.

__________________________________________________________________________________________________________________


Script
oracle /opt/oracle/scripts> more datapump_test.sh
#!/bin/bash
. $HOME/.bash_profile
set -x
export ORACLE_SID=klwtst
export ORAENV_ASK=NO;
. oraenv;
echo $ORACLE_SID
touch $ORACLE_SID
ls -l $ORACLE_SID
exit


Command-line output
oracle /opt/oracle/scripts> ./datapump_test.sh

+ export ORACLE_SID=klwtst
+ ORACLE_SID=klwtst
+ export ORAENV_ASK=NO
+ ORAENV_ASK=NO
+ . oraenv
++ case ${ORACLE_TRACE:-""} in
++ N=
++ C=
++ echo '\c'
++ grep c
++ N=-n
++ '[' /opt/oracle/product/11.1.0.7 = 0 ']'
++ OLDHOME=/opt/oracle/product/11.1.0.7
++ case ${ORAENV_ASK:-""} in
++ NEWSID=klwtst
++ export ORACLE_SID
+++ dbhome klwtst
++ ORAHOME=/opt/oracle/product/11.1.0.7
++ case $? in
++ ORACLE_HOME=/opt/oracle/product/11.1.0.7
++ export ORACLE_HOME
++ case ${LD_LIBRARY_PATH:-""} in
++ LD_LIBRARY_PATH=/opt/oracle/product/11.1.0.7/lib
++ export LD_LIBRARY_PATH
++ case "$OLDHOME" in
++ case "$PATH" in
+++ echo /usr/kerberos/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/bin:/usr/bin:/home/oracle/bin:/opt/oracle/product/11.1.0.7/bin:/home/oracle/bin:/opt/oracle/product/11.1.0.7/bin
+++ sed 's;/opt/oracle/product/11.1.0.7/bin;/opt/oracle/product/11.1.0.7/bin;g'
++ PATH=/usr/kerberos/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/bin:/usr/bin:/home/oracle/bin:/opt/oracle/product/11.1.0.7/bin:/home/oracle/bin:/opt/oracle/product/11.1.0.7/bin
++ export PATH
+++ LANG=C
+++ ulimit
++ ULIMIT=unlimited
++ '[' 0 = 0 -a unlimited '!=' unlimited ']'
++ ORABASE_EXEC=/opt/oracle/product/11.1.0.7/bin/orabase
++ '[' x '!=' x ']'
++ '[' -e /opt/oracle/product/11.1.0.7/bin/orabase ']'
++ '[' -x /opt/oracle/product/11.1.0.7/bin/orabase ']'
+++ /opt/oracle/product/11.1.0.7/bin/orabase
++ ORACLE_BASE=/opt/oracle/product
++ export ORACLE_BASE
++ echo 'The Oracle base for ORACLE_HOME=/opt/oracle/product/11.1.0.7 is /opt/oracle/product'
The Oracle base for ORACLE_HOME=/opt/oracle/product/11.1.0.7 is /opt/oracle/product
+ echo klwtst
klwtst
+ touch klwtst
+ ls -l klwtst
-rw-r--r-- 1 oracle oinstall 0 Mar 31 07:55 klwtst
+ exit


Cron entry
00 08 * * 1-5 /opt/oracle/scripts/datapump_test.sh >/opt/oracle/scripts/logs/datapump_test.txt


Cron log (empty)
-rw-r--r-- 1 oracle oinstall       0 Mar 31 08:00 datapump_test.txt

Re: Shell Script Doesn't Set Variables Correctly When Run From Cron [message #611252 is a reply to message #611250] Mon, 31 March 2014 08:18 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
you can capture the results using "script" at the start of the file invoked via cron

man script

oracle /opt/oracle/scripts> more datapump_test.sh
#!/bin/bash
script /tmp/capture.cron
. $HOME/.bash_profile
set -x
export ORACLE_SID=klwtst
export ORAENV_ASK=NO;
. oraenv;
echo $ORACLE_SID
touch $ORACLE_SID
ls -l $ORACLE_SID
exit
Re: Shell Script Doesn't Set Variables Correctly When Run From Cron [message #611254 is a reply to message #611252] Mon, 31 March 2014 08:29 Go to previous messageGo to next message
coreyr
Messages: 18
Registered: March 2014
Junior Member
Interesting. Thanks for letting me know about that.

Even though "capture.cron" was created, nothing was written to it.

____________________________________________________________________________________________________________________________________


Updated script:
#!/bin/bash
script /tmp/capture.cron     #added line
. $HOME/.bash_profile
set -x
export ORACLE_SID=klwtst
export ORAENV_ASK=NO;
. oraenv;
echo $ORACLE_SID
touch $ORACLE_SID
ls -l $ORACLE_SID
exit


Log file:
-rw-r--r-- 1 oracle oinstall     0 Mar 31 08:22 capture.cron

Re: Shell Script Doesn't Set Variables Correctly When Run From Cron [message #611255 is a reply to message #611254] Mon, 31 March 2014 09:55 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
[oracle@localhost ~]$ cat /tmp/cron_test.log
Script started on Mon Mar 31 07:54:01 2014
Running cron-job foo at Mon Mar 31 07:54:01 PDT 2014
+ export ORACLE_SID=orcl
+ ORACLE_SID=orcl
+ echo orcl
orcl
+ touch orcl
+ ls -l orcl
-rw-rw-r-- 1 oracle oracle 0 Mar 31 07:54 orcl
+ exit

Script done on Mon Mar 31 07:54:01 2014
[oracle@localhost ~]$ cat test.sh
#!/bin/bash
#script /tmp/capture_log.txt
echo "Running cron-job foo at $(date)"
set -x
export ORACLE_SID=orcl
echo $ORACLE_SID
touch $ORACLE_SID
ls -l $ORACLE_SID
exit
[oracle@localhost ~]$ crontab -l
54 7 * * * /usr/bin/script -c /home/oracle/test.sh /tmp/cron_test.log
[oracle@localhost ~]$
Re: Shell Script Doesn't Set Variables Correctly When Run From Cron [message #611256 is a reply to message #611255] Mon, 31 March 2014 10:23 Go to previous messageGo to next message
coreyr
Messages: 18
Registered: March 2014
Junior Member
That is successful.

___________________________________________________________________________________________________________________________


Updated script:
#!/bin/bash
#script /tmp/capture.cron
echo "Running cron-job datapump_test at $(date)"
set -x
export ORACLE_SID=orcl
echo $ORACLE_SID
touch $ORACLE_SID
ls -l $ORACLE_SID
exit


Crontab entry:
15 10 * * * /usr/bin/script -c /opt/oracle/scripts/datapump_test.sh /tmp/datapump_test.txt


Output:
Running cron-job datapump_test at Mon Mar 31 10:15:01 CDT 2014

+ export ORACLE_SID=orcl
+ ORACLE_SID=orcl
+ echo orcl
orcl
+ touch orcl
+ ls -l orcl
-rw-r--r-- 1 oracle oinstall 0 Mar 31 10:15 orcl
+ exit

Script done on Mon Mar 31 10:15:01 2014
Re: Shell Script Doesn't Set Variables Correctly When Run From Cron [message #611257 is a reply to message #611256] Mon, 31 March 2014 10:34 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
so now you know how to capture what occurs inside script invoked via cron
Re: Shell Script Doesn't Set Variables Correctly When Run From Cron [message #611258 is a reply to message #611257] Mon, 31 March 2014 10:42 Go to previous messageGo to next message
coreyr
Messages: 18
Registered: March 2014
Junior Member
Thanks for all the help, BlackSwan. I really appreciate it!
Re: Shell Script Doesn't Set Variables Correctly When Run From Cron [message #611994 is a reply to message #611258] Thu, 10 April 2014 12:35 Go to previous messageGo to next message
coreyr
Messages: 18
Registered: March 2014
Junior Member
Finally got back to the script and was able to get it to run successfully by doing the following:

1) Removed the sourcing of my profile;

2) Changed the call to oraenv to include the fully qualified path:
  . oraenv to . /usr/local/bin/oraenv

3) Edited the oraenv file so that the call inside it to dbhome used the fully qualified path:
ORAHOME=`dbhome "$ORACLE_SID"` to ORAHOME=`/usr/local/bin/dbhome "$ORACLE_SID"` 

Thanks again to BlackSwan and Michel for the assistance.

Re: Shell Script Doesn't Set Variables Correctly When Run From Cron [message #612002 is a reply to message #611994] Thu, 10 April 2014 12:58 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Thank you for your feedback.

Re: Shell Script Doesn't Set Variables Correctly When Run From Cron [message #612003 is a reply to message #612002] Thu, 10 April 2014 13:06 Go to previous messageGo to next message
coreyr
Messages: 18
Registered: March 2014
Junior Member
C'est moi.
Re: Shell Script Doesn't Set Variables Correctly When Run From Cron [message #612006 is a reply to message #611994] Thu, 10 April 2014 13:18 Go to previous message
EdStevens
Messages: 1376
Registered: September 2013
Senior Member
Alternatively, you could have avoided modifying the oracle-supplied 'oraenv' (as well as hard-coding the path to oraenv) by doing the following in your own script:


export PATH=/usr/local/bin:$PATH  #both oraenv and dbhome are (should be) located in /usr/local/bin
#-- the above only has to be set once in a script, regardless of how many times you may need to call oraenv
export ORAENV_ASK=NO
export ORACLE_SID=mydb
. oraenv
unset ORAENV_ASK


The above (minus the comments) is the standard block of code I put in all of my scripts.
Previous Topic: Redhat
Next Topic: Oracle Pre Installation check failure
Goto Forum:
  


Current Time: Thu Apr 18 13:37:52 CDT 2024