PHP error guide
Cron PHP environment variables are missing: meaning and fix
Error summary
Load a protected application environment explicitly or define the minimal variables in the service wrapper; never assume an interactive profile runs under cron.
What it means
Load a protected application environment explicitly or define the minimal variables in the service wrapper; never assume an interactive profile runs under cron.
What the error means
The scheduled process starts with a small environment and cannot see variables exported only by a login shell, terminal, or web-server service.
Why PHP produces it
Cron launches the command without sourcing the user’s interactive profile or the FPM pool environment.
PHP version notes
The behavior described for Cron PHP environment variables are missing applies to PHP 7.4–8.4 unless a narrower version is stated; exact wording can vary by SAPI and patch release.
Most common causes
- Secrets are exported only in .bashrc.
- The application depends on FPM env directives.
- HOME, locale, or application mode differs for the cron user.
Minimal examples
BAD — reproduces the problem
* * * * * source ~/.bashrc && php task.php
FIXED — safer pattern
* * * * * /srv/app/bin/run-cron-task
Step-by-step diagnosis
- Capture a redacted env listing from a one-time job.
- Compare the cron user and working directory with manual execution.
- Trace application bootstrap to the exact missing variable source.
Fixes
Use an explicit protected wrapper
Load site-scoped environment data with restrictive permissions before exec.
#!/bin/sh
set -a
. /srv/app/config/cron.env
set +a
exec /usr/bin/php8.3 /srv/app/bin/task.php
Common mistakes when fixing it
- Writing secrets directly into world-readable crontabs or logs.
- Sourcing an interactive profile with unrelated commands.
- Hardcoding production credentials in PHP.
How to prevent it
- Use one protected environment source per site.
- Validate required variables at process startup.
- Alert on scheduled task failures.
Web server / environment notes
cron, cli, linux. Cron provides only configured variables and a small default environment.
Tags: cron,cli,linux