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

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • 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. Maintaining Linux filesystems using “fsck” and “tune2fs”
  2. YUM command examples to install, remove and upgrade packages
  3. Oracle RMAN 12c – New Features
  4. Which SPFILE Parameter File is Impacted when Issuing an ALTER SYSTEM Command
  5. Find Oracle RAC OCR & voting disk location
  6. CentOS / RHEL LVM : Backing Up Volume Group Metadata
  7. How to configure VNC Server on CentOS/RHEL 6
  8. Oracle 12c Multitenant Architecture: Troubleshooting Ora-12518
  9. How to Change the VNC Server Resolution in Linux
  10. How to verify the integrity of a file with md5 checksum

You May Also Like

Primary Sidebar

Recent Posts

  • JavaFX ComboBox: Set a value to the combo box
  • Nginx load balancing
  • nginx 504 gateway time-out
  • Images preview with ngx_http_image_filter_module

© 2022 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright