• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Geek Diary

CONCEPTS | BASICS | HOWTO

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • Linux Services
    • VCS
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
    • Data Guard
  • DevOps
    • Docker
    • Shell Scripting
  • Interview Questions
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

Cron Script does not Execute as Expected from crontab – Troubleshoot

By admin

The Problem

Running a script using the cron service, that executes normally from the shell but does not exhibit the same behavior when running from crontab.

Root Cause

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.

The Solution

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.

NOTE: There is a space between the dot “.” and “/home..”

TIP: To better troubleshoot a crontab entry execution, you can change it to log the output to a file, for example:

[* * * * *] /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.

Filed Under: Linux, oracle

Some more articles you might also be interested in …

  1. UNIX / Linux : What is the correct permission of /tmp and /var/tmp directories
  2. How to monitor Undo Tablespace Usage and the Free Space in Oracle Database
  3. RMAN-06059 During RMAN Backup of archivelogs ( How to backup archivelogs moved to a different location)
  4. How to configure resource groups for MySQL Server running on Linux
  5. How to find docker storage device and its size (device mapper storage driver)
  6. CentOS / RHEL : Resize (extend) non-root EXT3/4 filesystem on non-LVM device (hard disk partition)
  7. RPM : package installation Error : cpio: read failed
  8. What is the Search Order for TNS files – listener.ora, sqlnet.ora, tnsnames.ora
  9. How to Disable “Predictable Network Interface Device Names” in CentOS/RHEL 7
  10. Linux OS Service ‘vncserver’

You May Also Like

Primary Sidebar

Recent Posts

  • Oracle Database – Configuring Secure Application Roles
  • Extend rule sets by using factors in Oracle Database Vault
  • What are Command Rules in oracle Database
  • Using Rule Sets in Oracle Database Vault
  • Archives
  • Contact Us
  • Copyright

© 2021 · The Geek Diary