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


 
Thread Tools Search this Thread
Operating Systems AIX Script is not executing as expected when I schedule it in cron
# 1  
Old 12-05-2016
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:

Code:
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.

Code:
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.
# 2  
Old 12-05-2016
Quote:
Originally Posted by Nagaraj R
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.
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:
Code:
        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?

Quote:
Originally Posted by Nagaraj R
and let me know if anything to be corrected in the script.
Gladly so: first, get rid of the obnoxious backticks and use POSIX-means to do it:

Code:
`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

Last edited by RudiC; 12-05-2016 at 08:07 AM.. Reason: Corrected /code tag.
# 3  
Old 01-23-2017
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.
Code:
* * * * * 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

Last edited by rbatte1; 01-24-2017 at 08:11 AM.. Reason: Added CODE tags
# 4  
Old 01-28-2017
Moderator's Comments:
Mod Comment Discussion about POSIX and backticks is now here: Advanced and Experts Forum


In answering this request by Michael Felt:
Quote:
but please come up with a catchy "subject line"
i tried my worst to make it as catchy as possible. ;-))

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Expect script not executing via cron

Hello All, I'm having an issue getting an expect script to run as a cron job. The script executes fin if I run it from the command line but I get nothing when trying to run it as a cron job. I've researched other forums and threads and there have been references to the environment, or lack... (16 Replies)
Discussion started by: KingT617
16 Replies

2. Shell Programming and Scripting

Script not executing using cron

Hi, I created a script which connects to database and update a table. This script is running fine when i run it manually but when i am trying to execute it scheduling in crontab.script is executing but Data is not getting updated. below is my script sqlplus test/##### >> test_feed.log <<!... (6 Replies)
Discussion started by: sv0081493
6 Replies

3. Shell Programming and Scripting

Binary Operator expected while executing the below shell script.

Hi Experts, Iam bit poor in shell scripting, Here my requirement is for generating an alert where the oracle database db_recovery_file_dest_size usage. If it reaches beyond 80% should recieve an alert through an email. Want to schedule this alert in cron. #!/bin/bash .... (9 Replies)
Discussion started by: Jagadish m
9 Replies

4. Windows & DOS: Issues & Discussions

Schedule script is not executing some times

Dear Experts, Once again i need your vital help to fix my issue, please do the needfull. Issue:- I have schedule one script on windows server to run's every 10 min.(Script do check the alert log file,if database found down it send the email) While i'm doing the database down manually its... (12 Replies)
Discussion started by: Mohammed Fareed
12 Replies

5. Shell Programming and Scripting

Executing a script from CRON behaves differently than terminal

Hi have a script which transferers from Microsoft server to Linux box. The scripts(ksh) is on Linux box. If I run script from terminal, it transfers files to directory. Where as If I run script from CRON. It does not. Here is the log of both: Terminal execution log:... (2 Replies)
Discussion started by: dipeshvshah
2 Replies

6. Shell Programming and Scripting

Problem with executing a shell script through the cron

Hi, I have a shell script as below: ORACLE_HOME=/usr/local/opt/oracle/product/dev export ORACLE_HOME PATH=$PATH:$ORACLE_HOME/bin:/usr/bin export PATH OUTFILE=/export/home/`basename $0`.out export OUTFILE export IDEN df -k . | tail -1 | read a b c d e f echo $a >> $OUTFILE echo $b... (4 Replies)
Discussion started by: Abhinav Pandey
4 Replies

7. Shell Programming and Scripting

General Q: how to run/schedule a php script from cron jobs maybe via bash from shell?

Status quo is, within a web application, which is coded completely in php (not by me, I dont know php), I have to fill out several fields, and execute it manually by clicking the "go" button in my browser, several times a day. Thats because: The script itself pulls data (textfiles) from a... (3 Replies)
Discussion started by: lowmaster
3 Replies

8. Shell Programming and Scripting

Script doesn't work as expected when run on cron

The script checks for free space stats on Oracle. If there are any tablespaces with more than 85% usage it prints the details of the tablespace. If all the tablespaces have more than 15% free space, then "All tablespaces have more than 15 pct free space" must be printed on the screen. When I run... (2 Replies)
Discussion started by: RoshniMehta
2 Replies

9. UNIX for Advanced & Expert Users

executing script by cron doesnt give me expected result

Hi frnds... I m facing very irritating problem already waisted my 2 days.. I have a following script..( i am pasting only the main code) ftp -ivn 213.194.40.77 <<FTP user $user $password binary cd $FileDir/out lcd $localpath get $file rename $FileDir/out/$file $FileDir/tmp/$file... (1 Reply)
Discussion started by: clx
1 Replies

10. HP-UX

executing shell script from the cron

This isn't the usual problem that a shell script runs from the command line and not the cron. It's a little different. Among other things, the shell scrip executes my .profile to set a bunch of variables. It then does an env to ensure that it ran OK. There are echos in the shell script and... (2 Replies)
Discussion started by: abNORMal
2 Replies
Login or Register to Ask a Question