I have the following cronjob defined in my /etc/crontab
Linux file:
* * * * * oracle cd "/home/oracle" && exp "user"/"password"@"TNS" owner="user" file="user".dmp log="user".log;
For purposes of exemplification, the asterisk symbol *
is being used in all fields to represent any time for the exp
command to execute.
When I try to execute this command directly via CLI, it works perfectly fine, that is, the backup and log files are created at the directory where the command is being executed in:
cd "/home/oracle" && exp "user"/"password"@"TNS" owner="user" file="user".dmp log="user".log
However, for some reason, when cron tries to execute it, it does not work. Why? I have already tried to define both PATH
and SHELL
variables at the top of the file, so that cron environment could see where to find this exp
executable. Just like this:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin:$ORACLE_HOME/bin
* * * * * oracle cd "/home/oracle" && exp "user"/"password"@"TNS" owner="user" file="user".dmp log="user".log;
I tried to replace the $ORACLE_HOME to its literal directory too, which is /opt/oracle
; it didn't work.
I either tried to replace the exp
command to where it is located according to its absolute path, just like this: /opt/oracle/bin/exp
; it didn't work nevertheless.
Cron is working though, because when I execute a simpler task (for instance, create a file in a directory where cd command took the cron into), it works fine.
* * * * * cd "/home/oracle" && touch text.txt;
Again, why is this happening? Am I missing some relevant information about how cron works?
Write a shell script to perform your export. Define your environment variables within that script; you can't do it in the crontab. Test that your shell script works independently, then use cron to run the shell script.
/home/oracle/export.sh:
crontab:
Also: