Results 1 to 8 of 8
Hi all,
I'm having this scenario which for the moment I cannot resolve.
I wrote a script to make a dump/export of the oracle database. and then put this entry ...
- 04-14-2011 #1Just Joined!
- Join Date
- May 2009
- Posts
- 17
Crontab strange behaviour
Hi all,
I'm having this scenario which for the moment I cannot resolve.
I wrote a script to make a dump/export of the oracle database. and then put this entry on crontab to be executed daily for example.
The script is like below:
Also the crontab entry:Code:cat /home/oracle/scripts/db_backup.sh #!/bin/ksh #Backup export database script #Created 05-04-2011 # * * * DATE=`date +%d%m%Y-%H%M%S` ARCHIVE_DIR=/home/oracle/arch SCRIPTS_DIR=/home/oracle/scripts USER=oracle PASS=XXXXXXXX ( echo "Starting database dump ..." date cd $ARCHIVE_DIR exp $USER/$PASS FILE=filename_$DATE.dmp log=logfile_$DATE.log echo "End of database dump ..." date ) | tee $ARCHIVE_DIR/exp_logfile-$DATE.log 2>&1
Code:host> crontab -l 00 11 * * * /home/oracle/scripts/db_backup.sh 2>&1
The crontab entry and also the script is executed as oracle user. When I execute directly from the shell the script is executed correctly and the dump is also generated ok.
But on the cron job I get only the log part "Starting/Stoping database dump ..." and not the export dump file and dump_log file: the "exp..." part.
What can be the problem?
Thanks
Enid
- 04-15-2011 #2Just Joined!
- Join Date
- Jun 2007
- Posts
- 16
oracle environment variables
I'm running out the door, but I believe that the oracle user under a cron environment needs all the necessary oracle variables that exist when you test execute the shell as oracle.
- 04-15-2011 #3
The way I usually do these is from root's crontab, like:
00 11 * * * su - oracle -c "/home/oracle/scripts/db_backup.sh 2>&1"
Takes care of what tueglee is saying, assuming you don't have some kind of interactive shell startup for oracle, to select the SID or whatever.
- 04-15-2011 #4Just Joined!
- Join Date
- Jan 2011
- Location
- Cambridge, Ontario, Canada
- Posts
- 22
Work-around
Without having to address the issue pointed out by truglee, perhaps you could work-around with:
00 11 * * * sh -c '/home/oracle/scripts/db_backup.sh 2>&1'
... where "sh" is your desired shell. Perhaps you could set the "SHELL" environment variable in your crontab. I doubt that these will work, but let us know what does work please.
- 04-15-2011 #5Just Joined!
- Join Date
- May 2009
- Posts
- 17
Hi all,
With some help on forums the problem was related to the environment variables
running
on the two occasions, shell and cron gave different env variables.Code:set
So I added at the beginning of the script this part:
Also running the cron job as root with this entry it works:Code:. /home/oracle/.profile
Everything OK , the dump export and the dump_log is created ok , but strange because the scripts stops/exits after the exp command and I don't get the last echo and date part of the script.Code:00 11 * * * su - oracle -c "/home/oracle/scripts/db_backup.sh 2>&1"
It is a minor problem , but just to understand why it stops there.
Regards
- 04-15-2011 #6
That's odd. Oracle does some cloak and dagger stuff (an ocfs fence-and-reboot doesn't yield any normal shutdown messages to syslogd, for instance), but this does yield the echo/date output when run on one of my 11.1 RAC systems.
BTW, the reason I like everything in root's crontab is because it lets me manage everything from one login, don't have to go traipsing through various user crontabs to manage everything. But if you want the DBA to manage the crontab, what you're doing is the way to go.Last edited by Mudgen; 04-15-2011 at 11:54 AM. Reason: add
- 04-15-2011 #7Just Joined!
- Join Date
- May 2009
- Posts
- 17
- 04-15-2011 #8Just Joined!
- Join Date
- Sep 2010
- Posts
- 2
Just to bring up another way to run a job without referencing the profile, you can export the needed environment variables within the script. I have found that oracle needs at least ORACLE_SID, ORACLE_HOME, SHLIB_PATH, and PATH to run.
Here is an example:
#!/usr/bin/ksh
export LOGDIR=/u01/app/oracle/log
export ORATAB=/var/opt/oracle/oratab
export ORACLE_SID=OATST
export ORACLE_HOME=`grep ^$ORACLE_SID $ORATAB|cut -d':' -f2`
export PATH=$ORACLE_HOME/bin:$PATH
export SHLIB_PATH=$ORACLE_HOME/lib:$SHLIB_PATH
x1=`echo "select * From dual;"|sqlplus -s /`
echo "`date` $x1" >> $LOGDIR/test_db_conn.log


Reply With Quote
