Installation:
composer require bentools/crontab-bundle:0.2.*
(No additional config needed with Symfony Flex.)
Define Crontab Jobs:
Create config/crontab.dist with entries like:
# Run daily at midnight
0 0 * * * php {%kernel.project_dir%}/bin/console app:daily-task
Preview Changes:
php bin/console crontab:update --dry-run --dump
Verify output matches expectations.
Apply:
php bin/console crontab:update
Confirm with crontab -l.
Schedule a Laravel command (e.g., app:cleanup) to run nightly:
0 3 * * * php {%kernel.project_dir%}/bin/console app:cleanup --force
Development Workflow:
--dry-run to test changes locally before deploying.crontab.dist in version control (exclude actual crontab via .gitignore).Deployment Workflow:
crontab.dist to repo.crontab:update post-deploy (via CI/CD or deployment script).Dynamic Configuration:
{%env('APP_ENV')%}) for environment-specific paths.Laravel Commands:
Ensure commands are callable via CLI (e.g., php artisan app:command).
Use absolute paths in crontab.dist (e.g., {%kernel.project_dir%}/bin/console).
Environment Awareness:
Use {%env('APP_ENV')%} to conditionally enable/disable jobs:
{% if env('APP_ENV') == 'production' %}
0 0 * * * php {%kernel.project_dir%}/bin/console app:backup
{% endif %}
Error Handling: Redirect output/errors to logs:
0 0 * * * php {%kernel.project_dir%}/bin/console app:task >> /var/log/app_cron.log 2>&1
Symfony Integration:
Combine with symfony/process for complex job orchestration.
Crontab Conflicts:
crontab -l > backup.cron) before running crontab:update.Path Resolution:
{%kernel.project_dir%} must resolve correctly. Test with:
php bin/console debug:container --parameter kernel.project_dir
Permissions:
crontab:update. Ensure:
bin/console.chmod +x bin/console).Template Parsing:
{% ... %} are replaced. Raw text is treated literally.Archived Package:
spatie/cron-to-expression (for parsing).crontab -l + echo redirection.Dry Run Mismatches:
Compare --dry-run --dump output with crontab -l after applying. Discrepancies may indicate:
Command Failures:
Check logs (/var/log/syslog or job-specific logs) for:
$PATH).Idempotency:
Use --force to overwrite existing entries:
php bin/console crontab:update --force
Job Descriptions:
Add comments to crontab.dist for clarity:
# Daily backup at 2 AM (Production only)
0 2 * * * php {%kernel.project_dir%}/bin/console app:backup
Testing: Simulate cron execution locally with:
* * * * * php {%kernel.project_dir%}/bin/console app:test-job --simulate
Alternatives for Complex Setups:
symfony/process to trigger jobs via HTTP (e.g., Laravel queues).Security:
Restrict crontab:update to deploy users via:
chmod 700 bin/console
How can I help you explore Laravel packages today?