Script is not executing as expected when I schedule it in cron

Hi,

I have a shell script which fetches the MRP status and the LAG status.

When I execute it manually like, sh <script_name>, it fetches the output as expected, but when I schedule through crontab, it's not working as expected.

Any help would be really appreciated.

Here is the code snippet:


for pass in $(cat /u/oracle/nagy/passwd.txt)
do

tns=`cat /u/oracle/nagy/passwd.txt | grep $pass | cut -d"@" -f2`
#env=`tnsping $tns | grep HOST | cut -d"-" -f2`
#env=$(echo ${env} | tr '[a-z]' '[A-Z]')
env=PROD
echo "Pass is: $pass"

if [[ "$tns" = "DR514" ]]; then
        echo "I am in IF Loop of LAG Check"
        status=`sqlplus -s "${pass} as sysdba" <<~EOF
        echo "After SQLPLUS in IF LAG CHECK"
        #set pages 999 long 90000 heading off
        SET MARKUP HTML ON SPOOL ON
        #spool /tmp/$FN
        echo "Spooling IF LAGCHECK"
        #@/u/oracle/nagy/check_loggap_DR514.sql
        #SET MARKUP HTML OFF SPOOL OFF
        #spool off
        spool /tmp/MRP_$tns.log
        echo "Spooling ID MRP"
        @/u/oracle/nagy/mrp.sql
        spool off
        exit;
        EOF`
else
        echo "I am in ELSE loop of LAG Check"
        status=`sqlplus -s "${pass} as sysdba" <<~EOF
        echo "After SQLPLUS in ELSE LAGCHECK"
        set pages 999 long 90000 heading off
        #SET MARKUP HTML ON SPOOL ON
        #spool /tmp/$FN
        echo ""Spooling ELSE LAGCHECK"
        #@/u/oracle/nagy/check_loggap.sql
        #SET MARKUP HTML OFF SPOOL OFF
        #spool off
        spool /tmp/MRP_$tns.log
        echo "Spooling ELSE MRP"
        @/u/oracle/nagy/mrp.sql
        spool off
        exit;
        EOF`
fi
done

The issue is, it's not getting into the line "status=`sqlplus -s "${pass} as sysdba" <<~EOF" at all, when I schedule it through cron, but manually it works.

Following is the sample output, when it's scheduled through crontab.


Pass is: sys/pr0t3ctSYS@DR508
I am in ELSE loop of LAG Check
Pass is: sys/pr0t3ctSYS@DR509_DG
I am in ELSE loop of LAG Check
Pass is: sys/pr0t3ctSYS@DR510
I am in ELSE loop of LAG Check
Pass is: sys/pr0t3ctSYS@DR511
I am in ELSE loop of LAG Check
Pass is: sys/pr0t3ctSYS@DR513
I am in ELSE loop of LAG Check
Pass is: sys/pr0t3ctSYS@DR514
I am in IF Loop of LAG Check


Please note that it's an AIX box.

Please help me out to get this rid of the weird situation and let me know if anything to be corrected in the script.

Search this forum for "Cron Problem Number One" to find the one or other gazillion of threads dealing with this. The main problem is this line:

        status=`sqlplus -s "${pass} as sysdba" <<~EOF

because: how should the system know where "sqlplus" is, without a PATH variable being set (which isn't set when executed from cron ), hmm?

Gladly so: first, get rid of the obnoxious backticks and use POSIX-means to do it:

`command1 | command2 | command3`      # wrong!
$(command1 | command2 | command3)     # correct!

Further advice might be given once you show us the complete script instead of a snippet. For a snippet of a script you only get a snippet of advice. ;-))

I hope this helps.

bakunin

The relevant part of /var/adm/cron/log may help.. it'll at least tell you if it's kicking off.

I've seen folks who source their profile before running a job, e.g.

* * * * * sh -c ". /home/oracle/.profile; sqlplus"

I would recommend putting that all into its own script instead if muddying the crontab file with the literal contents of what you're trying to do due to the aforementioned environment and variable expansion issues.

Remember that cron does not "log in" so all of the nice stuff that works for an interactive login to a shell must be dealt with and setup explicitly

Hth

In answering this request by Michael Felt:

i tried my worst to make it as catchy as possible. ;-))

bakunin