Symfony cron bundle for handle scheduled tasks consistently, parallel or across a cluster, like Symfony Messenger

Installs: 413

Dependents: 1

Suggesters: 0

Security: 0

Stars: 3

Watchers: 1

Forks: 1

Open Issues: 0


0.1.1 2020-01-18 12:05 UTC

This package is auto-updated.

Last update: 2021-04-18 15:12:40 UTC


This bundle provides interfaces for registering and handle scheduled tasks within your Symfony application.

Latest Stable Version Total Downloads Latest Unstable Version License


This is a more simpler alternative of existing cron bundle without doctrine deps. Here also added support middleware for customization handling cron jobs across a cluster install: (Send jobs to message queue, like Symfony Messenger; locking; etc.). This allow to limit the number of parallel running processes and prioritized it.


  • Not need doctrine/database.
  • Integration with Symfony Messenger.
  • Load a cron job from a different storage (config.yml, tagged services, commands).
  • Support many engines to run cron (in parallel process, message queue, consistently).
  • Support many types of cron handlers/command: (services, symfony commands, UNIX shell commands).
  • Middleware and customization.

Table of Contents


First way. Install system crontab

To regularly run a set of commands from your application, configure your system to run the oro:cron command every minute. On UNIX-based systems, you can simply set up a crontab entry for this:

*/1 * * * * /path/to/php /path/to/bin/console okvpn:cron --env=prod > /dev/null

Second way. Using supervisor

Setup Supervisor to run cron on demand.

sudo apt -y --no-install-recommends install supervisor

Create a new supervisor configuration.

sudo vim /etc/supervisor/conf.d/app_cron.conf

Add the following lines to the file.

command=/path/to/bin/console okvpn:cron --env=prod --demand

Registration a new scheduled task

To add a new scheduled task you can use tag okvpn.cron or using autoconfigure with interface Okvpn\Bundle\CronBundle\CronSubscriberInterface.



namespace App\Cron;

use Okvpn\Bundle\CronBundle\CronSubscriberInterface;

class MyCron implements CronSubscriberInterface // implements is not required, but helpful if yor are use autoconfigure
    public function __invoke(array $arguments = [])
        // processing...

    public static function getCronExpression(): string
        return '*/10 * * * *';

If you use the default configuration, the corresponding service will be automatically registered thanks to autoconfigure. To declare the service explicitly you can use the following snippet:

            - { name: okvpn.cron, cron: '*/5 * * * *' }

            - { name: okvpn.cron, cron: '*/5 * * * *', lock: true, async: true }

Possible options to configure with tags are:

  • cron - A cron expression, if empty, the command will run always.
  • lock - Prevent to run the command again, if prev. command is not finished yet. Possible value: true, {name: lock1, ttl: 300}. To use this option need to install symfony lock component
  • async - Run command in the new process without blocking main thread.
  • arguments - Array of arguments, used to run symfony console commands or pass arguments to handler.
  • priority - Sorting priority.
  • group - Group name, see Cron Grouping section.
  • messenger - Send jobs into Messenger Bus. Default false. You also can specify transport here {routing: async}, see Symfony Routing Messages to a Transport

Symfony console command

            - { name: console.command }
            - { name: okvpn.cron, cron: '*/5 * * * *' }

Via configuration / shell commands

      command: "php %kernel_project.dir%/bin/console cache:clear --env=prod" # Shell command 
      cron: "0 0 * * *"
      command: "bash /root/renew.sh > /root/renew.txt" # Shell command
      group: root # Filtering by group. You can run `bin/console okvpn:cron --group=root` under the root user 
      cron: "0 0 * * *"
      command: 'App\Cron\YouServiceName' # Your service name
      cron: "0 0 * * *"
      command: 'app:cron:sync-amazon-orders' # Your symfony console command name
      cron: "*/30 * * * *"
      async: true
      arguments: { '--transport': 15 } # command arguments or options

Full Configuration Reference

# Your config file
    lock_factory: ~ # The Service to create lock. Default lock.factory, see Symfony Lock component.
    timezone: ~ # default timezone, like Europe/Minsk. if null will use php.ini default
        enable: false # Enable symfony messenger
    # Default options allow to add define default policy for all tasks, 
    # For example to always run commands with locking and asynchronously
        async: true # Default false
        lock: true # Default false
        messenger: true # Handle all jobs with symfony messenger bus.
    # Stamps it's markers that will add to each tasks.
        - 'Packagist\WebBundle\Cron\WorkerStamp'

    tasks: # Defined tasks via configuration
        command: 'app:noaa:gfs-grib-download'
        cron: '34,45 */6 * * *'
        messenger: { routing: lowpriority } # See Messenger configuration
        lock: true
        arguments: { '--transport': '0p25' }
        # Here you can also add other custom options and create your own middleware.
        command: "bash /root/renew.sh > /root/renew.txt" # Shell command
        group: root # Group filter. You can run `bin/console okvpn:cron --group=root` under the root user 
        cron: "0 0 * * *"

Handle Cron Jobs via Symfony Messenger

To limit the number of parallel running processes you can handle the cron jobs in the queue using Symfony Messenger.

  1. Install Symfony Messenger
  2. Enable default route for cron job
# config/packages/messenger.yaml
            async: "%env(MESSENGER_TRANSPORT_DSN)%"
            lowpriority: "%env(MESSENGER_TRANSPORT_LOW_DSN)%"

            # async is whatever name you gave your transport above
            'Okvpn\Bundle\CronBundle\Messenger\CronMessage':  async
  1. Enable Messenger for cron.
# config/packages/cron.yaml
    # Required. messenger middleware is disable
        enable: true

    # Optional
        messenger: true # For handle all cron jobs with messenger
    # Optional 
            command: 'app:noaa:gfs-grib-download'
            cron: '34,45 */6 * * *'
#           messenger: true # OR
            messenger: { routing: lowpriority } # Send to lowpriority transport

More information how to use messenger here

Your own Scheduled Tasks Loaders

You can create custom tasks loaders, see example

<?php declare(strict_types=1);

namespace Packagist\WebBundle;

use Okvpn\Bundle\CronBundle\Loader\ScheduleLoaderInterface;
use Okvpn\Bundle\CronBundle\Model\ScheduleEnvelope;
use Okvpn\Bundle\CronBundle\Model;

class DoctrineCronLoader implements ScheduleLoaderInterface
     * @inheritDoc
    public function getSchedules(array $options = []): iterable
        // ... get active cron from database/etc.

        yield new ScheduleEnvelope(
            'yor_service_command_name', // A service name, (object must be have a __invoke method)
            // !!! Important. You must mark this service with tag `okvpn.cron_service` to add into our service locator.
            new Model\ScheduleStamp('*/5 * * * *'), // Cron expression
            new Model\LockStamp('yor_service_command_name'), // If you want to use locking
            new Model\AsyncStamp() // If you want to run asynchronously

        yield new ScheduleEnvelope(
            'app:cron:sync-amazon-orders', // Symfony console
            new Model\ScheduleStamp('*/5 * * * *'), // Cron expression
            new Model\LockStamp('sync-amazon-orders_1'), // If you want to use locking
            new Model\ArgumentsStamp(['--integration' => 1]), // Command arguments
            new Model\AsyncStamp() // If you want to run asynchronously

        yield new ScheduleEnvelope(
            'ls -l', // shell command
            new Model\ScheduleStamp('*/10 * * * *'),  // Cron expression
            new Model\ShellStamp(['timeout'=> 300]), // Run command as shell

        // ...

And register your loader.

        tags: [okvpn_cron.loader]

Handling cron jobs across a cluster or custom message queue

See example of customization one, two


MIT License.