Backup Does Not Run When Scheduled in CRON

You have created a script to perform the backup and have tested it to verify that it works as expected. However when the script is executed in CRON it either appears to not run or it runs with errors.

In many cases the problem is that the script does not EXPORT the proper environment variables. While CRON does run the script as the user who scheduled the job, it does not run execute the users .profile or .bash_profile.

In the following script we simply pull all of the environment variables that contain ORACLE.

[oracle@prod ~]$ cat crontest.sh
set | grep -i oracle > cronenv.txt

[oracle@prod ~]$

If we were to execute the command in a terminal session as the oracle user we would get the following output.

[oracle@prod ~]$ ./crontest.sh 
[oracle@prod ~]$ cat cronenv.txt 
GTK_RC_FILES=/etc/gtk/gtkrc:/home/oracle/.gtkrc-1.2-gnome2
HOME=/home/oracle
LOGNAME=oracle
MAIL=/var/spool/mail/oracle
ORACLE_BASE=/u01/app
ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1
ORACLE_SID=proddb
PATH=/u01/app/oracle/product/11.2.0/dbhome_1/bin:/usr/kerberos/bin:/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/home/oracle/bin
PWD=/home/oracle
USER=oracle
USERNAME=oracle
[oracle@prod ~]$

Notice that the ORACLE_* environment variables are present and the PATH includes the ORACLE_HOME/bin location. Below we set up a CRON job to run the above script every minute.

[oracle@prod ~]$ crontab -e
no crontab for oracle - using an empty one

*/1 * * * * /home/oracle/crontest.sh

crontab: installing new crontab
[oracle@prod ~]$ rm cronenv.txt 
[oracle@prod ~]$ 

Looking at cronenv.txt after CRON executed the script we see the following. Notice how none of the ORACLE_* environment variables are set.

[oracle@prod ~]$ cat cronenv.txt 
BASH_SOURCE=([0]="/home/oracle/crontest.sh")
HOME=/home/oracle
LOGNAME=oracle
PWD=/home/oracle
USER=oracle
_=/home/oracle/crontest.sh
You have new mail in /var/spool/mail/oracle
[oracle@prod ~]$ 

When scheduling scripts in CRON you must ensure the scripts sets the appropriate environment variables. Below we change the script to include the ORACLE_* environment variables and ensure that the PATH contains the ORACLE_HOME/bin.

[oracle@prod ~]$ vi crontest.sh 
[oracle@prod ~]$ cat crontest.sh 
export ORACLE_BASE=/u01/app
export ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1
export ORACLE_SID=proddb
export PATH=$ORACLE_HOME/bin:$PATH
set | grep -i oracle > /home/oracle/cronenv.txt

[oracle@prod ~]$ rm cronenv.txt 
[oracle@prod ~]$ 

Now when we look at the cronenv.txt after CRON executed the script we see that all of the appropriate environment variables are set.

[oracle@prod ~]$ cat cronenv.txt 
BASH_SOURCE=([0]="/home/oracle/crontest.sh")
HOME=/home/oracle
LOGNAME=oracle
ORACLE_BASE=/u01/app
ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1
ORACLE_SID=proddb
PATH=/u01/app/oracle/product/11.2.0/dbhome_1/bin:/usr/bin:/bin
PWD=/home/oracle
USER=oracle	
[oracle@prod ~]$ 

Leave a Reply

Your email address will not be published. Required fields are marked *