Linux / OS Fundamentals Series

⏰ A Top 1% Hands-On for Reproducing Yourself Why a Cron Job 'Should Work but Fails' — Crontab Never Inherits Your Shell's Environment at All

Introduction

This article is part of the Top 1% Series Complete Article Guide, and article 8.1 in the Linux/OS Fundamentals Series.

Prerequisite Knowledge

Getting the Big Picture

A script that runs fine when you execute it manually from the terminal can fail silently, with no error message at all, once you register it in crontab. The real cause of this is that cron runs a job without ever going through /etc/profile, ~/.bashrc, or ~/.bash_profile — the files loaded when a login shell starts up. The environment variables cron actually holds are limited to a bare minimum — something like PATH=/usr/bin:/bin.

graph TB
    subgraph Interactive["Interactive Shell"]
        Login["Log in"] --> Profile["Loads /etc/profile, ~/.bashrc"]
        Profile --> FullPath["Extended PATH<br/>(~/.local/bin, nvm, pyenv, etc.)"]
    end
    subgraph Cron["Cron Job"]
        CronStart["cron starts the job"] -->|"Never goes through<br/>any login files at all"| MinimalPath["Minimal PATH<br/>(/usr/bin:/bin only)"]
    end

Hands-On: Write Out Cron’s Environment Yourself and Compare It

Step 1: Write Out Your Interactive Shell’s Environment Variables

env > ~/interactive-env.txt

Step 2: Run That Same env Command as a Cron Job

Register a temporary job in crontab that runs every minute.

* * * * * env > /tmp/cron-env.txt 2>&1

Wait one minute, then confirm /tmp/cron-env.txt has been created.

Step 3: Compare the Two Environment Variable Dumps

diff ~/interactive-env.txt /tmp/cron-env.txt

Looking at this diff, you can confirm with your own eyes that PATH is drastically shorter, SHELL is set to /bin/sh (even if your interactive shell is bash or zsh), and any environment variable you configured yourself in your login shell simply doesn’t exist on the cron side at all.

Step 4: Actually Reproduce a Failure With a PATH-Dependent Script

Prepare a simple script that calls a command existing only on your interactive shell’s PATH (for instance, a script placed in ~/.local/bin, or a binary managed by nvm or pyenv), and first confirm it runs successfully when executed manually. Next, register that exact same script as a cron job, and confirm it fails silently, without even a “command not found” error ever being output.

Why Doesn't Even an Error Message Show Up?

By default, cron never displays a job’s standard output or standard error anywhere at all. In an environment with no mail delivery configured (MAILTO), even the fact that an error occurred never gets recorded anywhere — the failure is genuinely “silent.” The first step toward making this problem investigable is adding an explicit output redirect, something like >> /path/to/log 2>&1, to the job definition itself.

What a Pro Sees Here (Top 1% Understanding)

“It Should Work” Is Really a Mix-Up Over “Which Environment Did You Actually Confirm It In”

“It worked when I ran it manually” is, in reality, nothing more than confirming it worked under your interactive shell’s full set of environment variables. Whether it works under cron — an entirely different execution context — is a fact that needs to be confirmed separately, explicitly. Carrying the perspective that invisible preconditions like environment variables can change every time the execution context changes is the attitude a top-1% engineer brings to calmly handling this kind of “unexplained failure.”

Common Misconceptions and Pitfalls

Troubleshooting Perspective

  1. A cron job produces a different result than a manual run: Add a temporary line to the cron job’s definition that dumps environment variables, like env > /tmp/cron-env.txt, and compare it against your interactive shell’s environment.
  2. A cron job appears to be failing with no error: Add >> /path/to/log 2>&1 to the job’s definition, to record standard output and standard error.
  3. Only a specific command results in “command not found” under cron: Specify that command with its full path inside the script, or explicitly set PATH at the top of the script.

Summary

Takeaways to Apply Today

  1. When registering a cron job, either explicitly set PATH inside the script, or specify commands with their full path.
  2. Always set up redirection of standard output and standard error for a cron job, so a silent failure never slips past you.

References

Comments

Feel free to leave thoughts, questions, or corrections below (sign in with a GitHub account to comment or react).