Script not executing using cron


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script not executing using cron
# 1  
Old 10-10-2013
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

Code:
sqlplus test/##### >> test_feed.log <<!
prompt Disabling Trigger CHANGE_DATE_TRG...
ALTER TRIGGER CHANGE_DATE_TRG DISABLE;
prompt Updating Date in TEST_FEEDS Table..
UPDATE TEST_FEEDS SET NEXT_EXPECTED=trunc(SYSDATE-2),NEXT_APPLICABLE=trunc(SYSDATE-2) WHERE DATAFEED_APK IN ('OTC','OTW');
COMMIT;
prompt Date updated in Test_feeds table as below
select NEXT_EXPECTED,NEXT_APPLICABLE from TEST_FEEDS where DATAFEED_APK IN ('OTC','OTW');
prompt Enabling Trigger CHANGE_DATE_TRG...
ALTER TRIGGER CHANGE_DATE_TRG ENABLE;
!


Please help and suggest changes required in the script to get it execute through a cron.
Many thanks in advance.
# 2  
Old 10-10-2013
A typical problem when using cron like this is that the environment that you have set up when you log in is not inherited by jobs run by cron. Assuming that sqlplus isn't on your system's default command search path, you can probably fix this by changing the sqlplus at the start of your cron job to an absolute pathname for sqlplus.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 10-10-2013
Yes,
and you'll probably also need to export the ORACLE_HOME variable
(and add <ORACLE_HOME>/bin to the PATH).
This User Gave Thanks to radoulov For This Post:
# 4  
Old 10-10-2013
As suggested, my script should look like this can you please confirm

Code:
ORACLE_HOME=/test/orabase/product/9.2.0
PATH=$PATH:$ORACLE_HOME/bin
export ORACLE_HOME
export PATH
echo $ORACLE_HOME
echo $PATH
 
 
sqlplus test/##### >> test_feed.log <<!
prompt Disabling Trigger CHANGE_DATE_TRG...
ALTER TRIGGER CHANGE_DATE_TRG DISABLE;
prompt Updating Date in TEST_FEEDS Table..
UPDATE TEST_FEEDS SET NEXT_EXPECTED=trunc(SYSDATE-2),NEXT_APPLICABLE=trunc(SYSDATE-2) WHERE DATAFEED_APK IN ('OTC','OTW');
COMMIT;
prompt Date updated in Test_feeds table as below
select NEXT_EXPECTED,NEXT_APPLICABLE from TEST_FEEDS where DATAFEED_APK IN ('OTC','OTW');
prompt Enabling Trigger CHANGE_DATE_TRG...
ALTER TRIGGER CHANGE_DATE_TRG ENABLE;
!

# 5  
Old 10-10-2013
If in sqlplus test/##### you don't use a TNS alias/service name/sid,
you'll need to add ORACLE_SID as well:

Code:
ORACLE_HOME=/test/orabase/product/9.2.0
PATH=$PATH:$ORACLE_HOME/bin
ORACLE_SID=<your sid>
export ORACLE_HOME ORACLE_SID PATH
printf 'ORACLE_HOME is %s\nORACLE_SID is: %s\nPATH set to: %s\n' \
"$ORACLE_HOME" "$ORACLE_SID" "$PATH"
 
"$ORACLE_HOME"/bin/sqlplus test/##### >> test_feed.log <<!
prompt Disabling Trigger CHANGE_DATE_TRG...
ALTER TRIGGER CHANGE_DATE_TRG DISABLE;
prompt Updating Date in TEST_FEEDS Table..
UPDATE TEST_FEEDS SET NEXT_EXPECTED=trunc(SYSDATE-2),NEXT_APPLICABLE=trunc(SYSDATE-2) WHERE DATAFEED_APK IN ('OTC','OTW');
COMMIT;
prompt Date updated in Test_feeds table as below
select NEXT_EXPECTED,NEXT_APPLICABLE from TEST_FEEDS where DATAFEED_APK IN ('OTC','OTW');
prompt Enabling Trigger CHANGE_DATE_TRG...
ALTER TRIGGER CHANGE_DATE_TRG ENABLE;
!

You may also want to redirect stderr to test_feed.log:
Code:
>> test_feed.log 2>&1

This User Gave Thanks to radoulov For This Post:
# 6  
Old 10-10-2013
I will try this code and get back to you. Meanwhile could you please help me to understand below i didnt get it.

You may also want to redirect stderr to test_feed.log:

---------- Post updated at 05:43 AM ---------- Previous update was at 05:27 AM ----------

How about doing it this way
Code:
ORACLE_HOME=/test/orabase/product/9.2.0
PATH=$PATH:$ORACLE_HOME/bin
export ORACLE_HOME
export PATH
echo $ORACLE_HOME
echo $PATH

sqlplus >> test_feed.log <<!
CONNECT test/#####@sid

# 7  
Old 10-10-2013
With the following redirection test_feed.log will contain both stdout and stderr (standard output and standard error):
Code:
sqlplus >> test_feed.log 2>&1 <<! 
CONNECT test/#####@<tns alias>

If you don't want to export the ORACLE_SID variable for whatever reason
and you want to use the above syntax, you'll need to pass a TNS service descriptor or the EZ-connect string (not the sid).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

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... (3 Replies)
Discussion started by: Nagaraj R
3 Replies

2. 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

3. Shell Programming and Scripting

"if" Loop not working when executing script using cron

I am facing this weird issue where the script is working fine from the command line but when I am executing it from cron though it is working fine but the "if" loop is processing else part though I know that the if part of the logic is true and ideally the loop should execute the if portion. ... (3 Replies)
Discussion started by: sk2code
3 Replies

4. UNIX for Dummies Questions & Answers

Cron shell script not executing diskutil command

I'm trying to learn how to use cron for repetative tasks. I have an external disk that needs to be unmounted and remounted every hour due to some problems that a backup utility (specifically, TimeMachine) is having repeatedly accessing the device. I've created a shell script that will find the... (3 Replies)
Discussion started by: illuminate
3 Replies

5. Shell Programming and Scripting

Cron job not executing

I need to add 10 records to database from a file /tmp/authlist.log(contains insert into table sql commands) When i execute the following script manually its executing and working fine. the same is not getting executed when i try to execute using crontab vi /tmp/test1.sh #!/bin/sh... (4 Replies)
Discussion started by: kalyankalyan
4 Replies

6. 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

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

8. 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

9. Shell Programming and Scripting

executing a cron

Hi Gurus I have a requirement to execute a cron job every 30 min until 8pm in the night and again start at 8 am in the morning. How would I add the cron entry for this requirements. all inputs are appreciated. Thanks in advance (8 Replies)
Discussion started by: ragha81
8 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