← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Cron Jobs

linux PHP 5.0+ Beginner

Also Known As

crontab scheduled tasks cron

TL;DR

Scheduled tasks defined in a crontab file, executing commands at specified time intervals — from once a minute to once a year.

Explanation

Cron syntax: five fields (minute hour day month weekday) followed by the command. Special strings (@hourly, @daily, @weekly, @monthly, @reboot) simplify common schedules. Cron runs in a minimal environment — PATH is different from interactive shells, so use absolute paths. Output goes to the user's email unless redirected. PHP cron jobs should use the CLI SAPI, not the web server. Consider systemd timers or a task scheduler like Laravel Scheduler for more complex needs.

Diagram

flowchart LR
    CRON[cron daemon] -->|reads| CRONTAB[crontab -e]
    CRONTAB --> SYNTAX[minute hour day month weekday command]
    subgraph Examples
        EV_MIN[every minute: * * * * *]
        EV_HOUR[every hour: 0 * * * *]
        EV_DAY[daily 2am: 0 2 * * *]
        EV_WEEK[weekly Mon: 0 0 * * 1]
    end
    CRON --> EXEC[Execute command]
    EXEC -->|stdout| MAIL[Email to user<br/>or redirect to log]
    subgraph Issues
        OVERLAP[Job overlaps<br/>previous still running]
        ENV2[Wrong environment<br/>PATH not set]
        OVERLAP & ENV2 -->|fix with| LOCKFILE[flock or systemd timers]
    end
style EXEC fill:#238636,color:#fff
style OVERLAP fill:#f85149,color:#fff
style LOCKFILE fill:#1f6feb,color:#fff

Common Misconception

Cron runs in the same environment as your shell — cron has a minimal PATH; always use absolute paths to binaries and PHP.

Why It Matters

Misconfigured cron jobs silently fail or run as the wrong user — a missed database cleanup or report generation creates data accumulation or business problems.

Common Mistakes

  • Using relative paths — cron's PATH does not include /usr/local/bin; use /usr/bin/php explicitly.
  • Not redirecting output — all stdout/stderr goes to cron email which is often unmonitored.
  • Overlapping executions — a slow job runs again before the previous one completes; use file locking.
  • Running cron jobs as root when a less privileged user is sufficient.

Code Examples

✗ Vulnerable
# Broken cron — relative path, no output handling:
*/5 * * * * php /var/www/app/artisan schedule:run
# Fails silently: 'php' not found in cron PATH
# No output captured — errors invisible
✓ Fixed
# Correct cron entry:
*/5 * * * * /usr/bin/php /var/www/app/artisan schedule:run \
    >> /var/log/app/schedule.log 2>&1

# With flock to prevent overlap:
*/5 * * * * flock -n /tmp/schedule.lock \
    /usr/bin/php /var/www/app/artisan schedule:run >> /var/log/app/schedule.log 2>&1

Added 15 Mar 2026
Edited 22 Mar 2026
Views 29
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 1 ping S 1 ping S 0 pings M 0 pings T 1 ping W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 2 pings F 0 pings S 0 pings S 1 ping M 1 ping T 0 pings W 0 pings T
No pings yet today
No pings yesterday
Amazonbot 7 Perplexity 7 Google 4 Unknown AI 2 Ahrefs 2 SEMrush 1
crawler 20 crawler_json 3
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Low
⚡ Quick Fix
Add MAILTO='' at the top of your crontab to suppress email on success; log all output to a file; use a cron management tool (Crunz, Laravel Scheduler) for complex schedules
📦 Applies To
PHP 5.0+ cli laravel symfony
🔗 Prerequisites
🔍 Detection Hints
No output logging from cron jobs; cron running as root; no overlap prevention for long-running jobs; no alerting on cron failure
Auto-detectable: ✗ No crontab crunz laravel-scheduler
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Medium ✗ Manual fix Fix: Medium Context: File
CWE-732

✓ schema.org compliant