Running a script using the cron service, that executes normally from the shell but does not exhibit the same behavior when running from crontab.
One of the most frequent causes for the crontab job not being correctly executed is that a cronjob does not run under the user’s shell environment. Another reason can be – not specifying the absolute path of the commands used in the script. When the script is run manually the environment variable such as PATH can be different than when running from the cron. So it is always recommended to include the absolute paths of the commands used in the script.
For that reason, any environment variables for the user that are present in a normal shell will not be available during cron job execution unless they are imported by the script explicitly.
For example, if the shell has an ORACLE_HOME variable defined and includes it in the PATH environment variable and the script makes use of those variables, the script will execute in the sell, but when running from crontab the script will have no knowledge of those variables by default.
Define or import ORACLE_HOME and the complete PATH variable in the cron script file, as you see in when inside the oracle user shell (oracle_user_shell> echo $PATH).
A good practice is to always import the user environment at the beginning of the script script.sh with the command:
#!/bin/bash . /home/oracle/.bashrc [rest of script]
This will read the /home/oracle/.bashrc and import the environment within. Depending on the user’s environment, it can also be /home/oracle/.bash_profile or other files.
[* * * * *] /home/oracle/script.sh 2> /tmp/crontab_script_log.txt 2>&1
Note: Replace [* * * * *] with the correct execution times for your case.
You can then check /tmp/crontab_script_log.txt for the output of the execution. If any variables are undefined or if the script has other errors, the output will make it easier to find the cause of the problem.