Installation:
composer require draw/cron-bundle
Add the bundle to config/bundles.php:
return [
// ...
Draw\CronBundle\DrawCronBundle::class => ['all' => true],
];
Configure:
Add the bundle configuration to config/packages/draw_cron.yaml (or merge into config/packages/dev/your_app.yaml):
draw_cron:
jobs:
my_first_job:
description: "My first cron job"
command: "php bin/console my:command"
expression: "* * * * *"
enabled: true
First Use Case: Run the dump command to generate a cron file:
bin/console draw:cron:dump-to-file /etc/cron.d/myapp
This creates a file with the configured cron jobs in the standard cron format.
%cron.context.enabled%) to toggle jobs per environment (e.g., dev, prod).
draw_cron:
jobs:
analytics_job:
enabled: "%kernel.debug%" # Disabled in production
console.execution parameter for cross-environment compatibility:
command: "%cron.console.execution% my:command --env=%kernel.environment%"
Development:
cron.dev) and manually adding it to your system’s crontab.enabled: false for jobs that shouldn’t run locally.Deployment:
draw:cron:dump-to-file into your deployment script (e.g., Ansible, Deployer) to update /etc/cron.d/your_app atomically.task('deploy:cron', function () {
run('cd {{release_path}} && bin/console draw:cron:dump-to-file /etc/cron.d/{{name}}');
});
CI/CD:
$output = shell_exec('bin/console draw:cron:dump-to-file /tmp/cron.test');
$this->assertStringContainsString('* * * * *', $output);
command: ">> /var/log/myapp_cron.log 2>&1").command: "%cron.console.execution% my:command --env=%kernel.environment% APP_ENV=%kernel.environment%"
command: "@=service('kernel').getProjectDir().'/bin/console my:command'"
File Permissions:
www-data) has write permissions.chmod 644 /etc/cron.d/your_app or deploy as root with sudo.Parameter Resolution:
%cron.console.execution%) cause silent failures. Always define them in parameters.yaml:
parameters:
cron.console.execution: "php %kernel.project_dir%/bin/console"
Cron Syntax Errors:
*/99 * * * *) are dumped as-is but may break cron. Validate expressions in tests or use a library like cron-validator.Overwrite Behavior:
--force (if available) or handle the exception in deployment scripts:
bin/console draw:cron:dump-to-file /etc/cron.d/myapp || true
/tmp/cron.debug) and manually inspect the output before deploying.bin/console debug:container --parameter=cron.console.execution
Custom Formatters:
Draw\CronBundle\Command\DumpToFileCommand class.Dynamic Job Loading:
JobLoader service and injecting it into the bundle’s CronManager.Environment-Specific Files:
enabled flag to route jobs to different files per environment:
draw_cron:
jobs:
dev_job:
enabled: "%kernel.debug%"
output: ">/tmp/dev_cron.log"
prod_job:
enabled: "!%kernel.debug%"
output: ">/var/log/prod_cron.log"
# ansible-playbook.yml
tasks:
- name: Update cron file
command: "bin/console draw:cron:dump-to-file /etc/cron.d/myapp"
when: cron_jobs_changed.stat.exists and cron_jobs_changed.stat.mtime < now()
draw_cron:
jobs:
backup_job:
description: "Daily backup at 2 AM"
# Custom comment
command: "php bin/console backup:run"
expression: "0 2 * * *"
Output:
# Custom comment
#Description: Daily backup at 2 AM
0 2 * * * php bin/console backup:run
How can I help you explore Laravel packages today?