Magento 2.4.6 CLI Command: bin/magento cron:run

Introduction

The bin/magento cron:run command is the central entry point for Magento’s cron execution system. It triggers tasks defined in crontab.xml and manages how Magento cron group execution works across various automated processes.

In this tenth part of the Magento 2.4.6 Cron Series, we explore its purpose, Magento cron command syntax, execution flow, database interactions (especially the cron_schedule table), and troubleshooting methods.

1. Purpose of bin/magento cron:run

The bin/magento cron:run command starts the internal cron execution engine, activating the ProcessCronQueueObserver described in Part 4.

This command plays a vital role in cron execution, log handling, and Magento cron group execution.

Key Functions

  • Schedules Jobs: Populates entries inside the cron_schedule table.
  • Executes Jobs: Runs PHP classes defined in cron jobs.
  • Logs Results: Stores output in log files and the cron_schedule table.
  • Group Execution: Focused Magento cron group execution using the –group flag.

2. Magento Cron Command Syntax

Magento provides a clean, structured syntax for controlling cron operations:

php bin/magento cron:run [–group=] [–bootstrap=] [–verbose]

Options Explained

  • –group= → For targeted Magento cron group execution
  • –bootstrap= → Advanced handling
  • –verbose → Debug-level cron execution output

The clarity of the Magento cron command syntax makes it easy to test, debug, and identify failures.

3. Example Usage

Run all jobs

php bin/magento cron:run

Run index group

php bin/magento cron:run –group=index

Verbose mode

php bin/magento cron:run –verbose >> var/log/cron_verbose.log

These commands are essential when validating cron execution or troubleshooting issues.

4. Use Cases

Regular Cron Execution

* * * * * php /path/to/magento/bin/magento cron:run >> /path/to/magento/var/log/magento.cron.log 2>&1

This ensures ongoing cron execution every minute.

Manual Testing

php bin/magento cron:run –group=custom_inventory

Useful for validating Magento cron group execution.

Debugging

php bin/magento cron:run –verbose

Shows detailed explanations of Magento cron command syntax failures and SQL issues.

Hello Store Owners, ready to take your business online or upgrade your existing store?

Get high-quality Website Development and Optimization Services designed to boost performance, speed, and sales.

Let’s connect with us and build something great together.

Contact Us

5. Database Interactions: cron_schedule Table

A crucial backend component of cron is the cron_schedule table, which stores job history and status updates.

Table Structure

Field Type Description
schedule_id int Primary key
job_code varchar(255) Job identifier
status varchar(20) pending, running, success, error
executed_at timestamp Start time
finished_at timestamp End time

The cron_schedule table is the backbone of monitoring cron execution.

Sample Query

SELECT
    job_code, 
    status, 
    executed_at, 
    finished_at
FROM cron_schedule
ORDER BY executed_at DESC
LIMIT 10;

Use this to track job flow after running bin/magento cron:run.

6. Sample Code: Triggering a Custom Cron

class InventorySync
{
    public function execute()
    {
        try {
            // Simplified process
        } catch (\Exception $e) {
            throw $e;
        }

        return $this;
    }
}

This custom logic is executed during cron execution under specific groups.

7. Workflow Flowchart

Run bin/magento cron:run

ProcessCronQueueObserver

Read crontab.xml

Update cron_schedule table

Execute Job Classes

Log Results

This flow shows where Magento cron group execution fits into the system.

8. Troubleshooting Common Errors

Stuck Jobs

Clear bad entries from the cron_schedule table:

DELETE FROM cron_schedule WHERE status=’running’;

Permission Issues

Ensure server user matches cron user.

SQL Errors

Fixes available through patches like ACSD-53347.

9. Best Practices

  • Run cron every minute for stable cron execution
  • Use the –group flag for cleaner Magento cron group execution
  • Monitor magento.cron.log + cron_schedule table
  • Test Magento cron command syntax in staging before production
  • Apply Magento 2.4.6 patches for reliability

Engagement Tip

Try running:

php bin/magento cron:run –group=index

Then check the cron_schedule table to see what executed.

What’s Next?

This post explored the Magento 2.4.6 CLI command bin/magento cron:run and how it powers cron execution.

In Part 11: Magento 2.4.6 CLI Command: bin/magento cron:install ↗, we’ll cover:

  • automating cron installation
  • ensuring stable cron execution
  • understanding generated crontab entries
  • improving long-term cron reliability

Stay tuned!