Debugging Cron Jobs

Systematic approach to troubleshoot and fix cron job issues.

Cron job not working? Follow this step-by-step debugging process to identify and fix the issue quickly.

Step 1: Verify the Basics

✅ Is cron daemon running?

ps aux | grep cron | grep -v grep systemctl status cron # or crond on some systems

✅ Is your job in the crontab?

crontab -l

Should show your job. If empty, you need to add it.

✅ Does the command work manually?

# Copy your cron command and run it directly /path/to/your/script.sh

Step 2: Check Logs

System Cron Logs

# Check if job is being triggered sudo tail -f /var/log/cron sudo tail -f /var/log/syslog | grep CRON # On some systems journalctl -f -u cron

Look for: Lines showing your job being executed or error messages

Application Logs

# Add logging to your cron job * * * * * /path/to/script.sh >> /var/log/my-cron.log 2>&1 # Check the output tail -f /var/log/my-cron.log

Step 3: Test Timing

Quick Test Job

Add a simple test job that runs every minute:

* * * * * echo "$(date): Test job running" >> /tmp/cron-test.log

Wait 2-3 minutes, then check: cat /tmp/cron-test.log

Step 4: Fix Environment Issues

🚨 Common Environment Problems

PATH Issues

# Add to top of crontab PATH=/usr/local/bin:/usr/bin:/bin

Working Directory

# Use full paths or cd first 0 1 * * * cd /home/user && ./script.sh

Environment Variables

# Debug what's available * * * * * env > /tmp/cron-env.txt

Step 5: Validate Syntax

Cron Expression Validation

Use our generator to validate your cron expression:

Command Validation

# Test exact command from cron sudo -u yourusername /bin/bash -c "your-command-here"

Debugging Checklist

Complete Debugging Checklist

Advanced Debugging

Wrapper Script Technique

Create a wrapper script for better debugging:

#!/bin/bash # /home/user/cron-wrapper.sh echo "$(date): Starting cron job" >> /var/log/my-cron-debug.log echo "Environment:" >> /var/log/my-cron-debug.log env >> /var/log/my-cron-debug.log echo "Working directory: $(pwd)" >> /var/log/my-cron-debug.log # Run your actual command /path/to/your/real-script.sh >> /var/log/my-cron-debug.log 2>&1 echo "$(date): Cron job finished with exit code: $?" >> /var/log/my-cron-debug.log

Email Debugging

Set up email alerts for cron output:

# Add to top of crontab [email protected] # Your cron job (output will be emailed) 0 2 * * * /path/to/script.sh

🎯 Still Having Issues?

If your cron job still isn't working after following this guide, check these specific troubleshooting pages: