Skip to content
intermediate Phase 65 · Cron System

Cron Debugging — Logs, Status, and Performance

Debugging cron jobs in Magento 2: cron logs, schedule status monitoring, common cron issues, and performance debugging

45m
1 problems
Topic Progress 0%

Cron Logs and Monitoring

Magento Cron Log

Magento logs cron execution to var/log/cron.log:

tail -f var/log/cron.log

Database Monitoring

-- Recent cron executions
SELECT job_code, status, scheduled_at, executed_at, finished_at
FROM cron_schedule
ORDER BY executed_at DESC
LIMIT 20;

-- Running jobs
SELECT * FROM cron_schedule WHERE status = 'running';

-- Failed jobs in last 24 hours
SELECT job_code, messages, executed_at
FROM cron_schedule
WHERE status = 'error'
AND executed_at > DATE_SUB(NOW(), INTERVAL 24 HOUR);

-- Job execution count by status
SELECT
    job_code,
    SUM(CASE WHEN status = 'success' THEN 1 ELSE 0 END) as ok,
    SUM(CASE WHEN status = 'error' THEN 1 ELSE 0 END) as errors,
    SUM(CASE WHEN status = 'missed' THEN 1 ELSE 0 END) as missed
FROM cron_schedule
WHERE executed_at > DATE_SUB(NOW(), INTERVAL 7 DAY)
GROUP BY job_code;

Cron Run Log

Enable detailed cron logging in app/etc/env.php:

return [
    'cron_run_path' => 'var/log/cron.log',
];

Common Cron Issues

Issue: Cron Not Running

Symptoms:

  • cron_schedule table has no new entries
  • Jobs stay in pending status

Diagnosis:

# Check system crontab
crontab -l

# Check if cron:run works
php bin/magento cron:run -vvv

# Check for PHP errors
php bin/magento cron:run 2>&1 | head -50

Fix:

# Add to system crontab
* * * * * /usr/bin/php /var/www/html/bin/magento cron:run

Issue: Jobs Running Too Long

Symptoms:

  • Schedule marked as missed while previous run still active
  • Lock files accumulate

Diagnosis:

SELECT job_code, executed_at, finished_at,
    TIMESTAMPDIFF(MINUTE, executed_at, finished_at) as duration_min
FROM cron_schedule
WHERE status = 'running'
AND executed_at < DATE_SUB(NOW(), INTERVAL 1 HOUR);

Fix:

  • Optimize job logic
  • Split into smaller jobs
  • Increase schedule_lifetime

Issue: Lock Files Not Released

# Check locks
ls -la var/cache/magento/cron/lock/

# Remove stale locks
rm var/cache/magento/cron/lock/*

Issue: Schedule Generation Gaps

-- Find gaps in schedule
SELECT scheduled_at, LEAD(scheduled_at) OVER (ORDER BY scheduled_at) as next_run
FROM cron_schedule
WHERE job_code = 'my_job';

Cron Performance Debugging

Profiling Cron Jobs

namespace Vendor\Module\Cron;

class PerformanceTest
{
    public function __construct(
        private \Psr\Log\LoggerInterface $logger
    ) {}

    public function execute(): void
    {
        $start = microtime(true);
        $memoryStart = memory_get_usage();
        
        $this->heavyOperation();
        
        $elapsed = microtime(true) - $start;
        $memoryUsed = memory_get_usage() - $memoryStart;
        
        $this->logger->info('Cron completed', [
            'duration' => round($elapsed, 4),
            'memory_mb' => round($memoryUsed / 1024 / 1024, 2)
        ]);
    }
}

Performance Metrics Query

SELECT
    job_code,
    COUNT(*) as runs,
    ROUND(AVG(TIMESTAMPDIFF(SECOND, executed_at, finished_at)), 2) as avg_seconds,
    ROUND(MAX(TIMESTAMPDIFF(SECOND, executed_at, finished_at)), 2) as max_seconds,
    ROUND(MIN(TIMESTAMPDIFF(SECOND, executed_at, finished_at)), 2) as min_seconds
FROM cron_schedule
WHERE status = 'success'
AND executed_at > DATE_SUB(NOW(), INTERVAL 7 DAY)
GROUP BY job_code
ORDER BY avg_seconds DESC;

Slow Cron Job Detection

-- Jobs taking more than 5 minutes
SELECT job_code, executed_at,
    TIMESTAMPDIFF(SECOND, executed_at, finished_at) as duration
FROM cron_schedule
WHERE status = 'success'
AND TIMESTAMPDIFF(SECOND, executed_at, finished_at) > 300
ORDER BY duration DESC;

Monitoring Dashboard

-- Last 24 hours summary
SELECT
    DATE(executed_at) as day,
    COUNT(*) as total_runs,
    SUM(CASE WHEN status = 'success' THEN 1 ELSE 0 END) as success,
    SUM(CASE WHEN status = 'error' THEN 1 ELSE 0 END) as errors
FROM cron_schedule
WHERE executed_at > DATE_SUB(NOW(), INTERVAL 24 HOUR)
GROUP BY DATE(executed_at);

Cron Debugging Tools

CLI Debugging

# Run specific cron job with verbose output
php bin/magento cron:run --group default 2>&1

# Check cron status
php bin/magento cron:status

# List all configured cron jobs
php bin/magento cron:run --help

ObjectManager for Debug

// Check registered cron jobs
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cronConfig = $objectManager->get(\Magento\Cron\Model\ConfigInterface::class);
$jobs = $cronConfig->getJobs();
print_r($jobs);

Observer for Debugging

<!-- Vendor/CronDebug/etc/events.xml -->
<event name="*">
    <observer name="cron_debug"
              instance="Vendor\CronDebug\Observer\CronDebug"/>
</event>
public function execute(Observer $observer)
{
    $name = $observer->getEvent()->getName();
    if (strpos($name, 'cron_') === 0) {
        $this->logger->debug('Cron event: ' . $name);
    }
}

Environment Variables

# Enable cron debugging
MAGE_DEBUG_LOG=true php bin/magento cron:run

# Check cron configuration
php bin/magento config:show | grep cron

Practice Problems

0 / 1 solved
Cron Job Timeout

A cron job runs for 30 minutes and causes missed schedules. Profile and optimize it.

Quiz

1. Where does Magento log cron execution?

Question 1 options

2. What does a 'missed' status indicate?

Question 2 options

3. How do you check if system cron is configured?

Question 3 options

4. Where are cron lock files stored?

Question 4 options

Flashcards

Question

Where is the cron log file?

Answer

var/log/cron.log

Question

What SQL finds slow cron jobs?

Answer

SELECT from cron_schedule WHERE TIMESTAMPDIFF > threshold

Question

How to remove stale lock files?

Answer

rm var/cache/magento/cron/lock/*

Question

How to run cron with verbose output?

Answer

php bin/magento cron:run -vvv

Question

How to check cron job status?

Answer

php bin/magento cron:status or query cron_schedule table

Revision Notes

Key Takeaways

  • 1. Monitor cron via var/log/cron.log and cron_schedule SQL queries
  • 2. Common issues: cron not running, jobs too long, lock files stuck
  • 3. Profile cron jobs with microtime and memory tracking
  • 4. Use SQL queries to detect slow jobs and missed schedules
  • 5. Debug with CLI flags, ObjectManager inspection, and event observers
  • 6. Set up monitoring dashboards with cron_schedule aggregation queries

Interview Tips

  • Describe a systematic approach to debugging cron issues
  • Explain how to profile cron job performance
  • Discuss common cron pitfalls and their solutions
  • Know the SQL queries for monitoring cron health

Cheat Sheet

Cron Debugging Cheat Sheet

Logs:
var/log/cron.log

SQL monitoring:

-- Recent runs
SELECT * FROM cron_schedule ORDER BY executed_at DESC;
-- Errors
SELECT * FROM cron_schedule WHERE status = 'error';
-- Slow jobs
SELECT * FROM cron_schedule
WHERE TIMESTAMPDIFF(SECOND, executed_at, finished_at) > 300;

CLI:
cron:run -vvv, cron:status

Lock files:
var/cache/magento/cron/lock/

Common fixes:

  1. Not running: check crontab -l
  2. Too long: optimize or split job
  3. Locks stuck: remove lock files
  4. Missed: reduce generate_every, increase lifetime