Installation
composer require dz/easy-cron-manager
Add the bundle to AppKernel.php:
new DZ\CronManagerBundle\DZCronManagerBundle(),
First Use Case Inject the service into a controller or service:
$cronManager = $this->get('dz_cron.manager');
$cronManager->parseCronFile(); // Parse existing crontab
$output = $cronManager->getOutput(); // Get parsed output
Where to Look First
dz_cron.manager (core service for CRUD operations).bin/console for built-in CLI tools (if any exist).config.yml or config/packages/dz_cron_manager.yaml (if applicable).Adding a Cron Job
Define jobs in a YAML/JSON file (e.g., config/cron_jobs.yml):
jobs:
my_job:
command: "php bin/console my:command"
schedule: "0 * * * *" # Every hour
user: "www-data"
Load and apply via:
$cronManager->loadFromYaml('config/cron_jobs.yml');
$cronManager->writeToCrontab(); // Sync to system crontab
Dynamic Job Management Use the service to programmatically add/remove jobs:
$job = new \DZ\CronManagerBundle\Entity\CronJob();
$job->setCommand('php bin/console my:dynamic-command');
$job->setSchedule('*/5 * * * *'); // Every 5 minutes
$cronManager->addJob($job);
$cronManager->writeToCrontab();
Event-Driven Triggers
Hook into Symfony events (e.g., kernel.terminate) to sync jobs on app shutdown:
$this->get('dz_cron.manager')->writeToCrontab();
CLI Integration Create custom commands to manage jobs:
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class SyncCronCommand extends Command
{
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->get('dz_cron.manager')->writeToCrontab();
$output->writeln('Cron jobs synced!');
}
}
%kernel.environment% in YAML to define environment-specific jobs.writeToCrontab() in a try-catch to log failures:
try {
$cronManager->writeToCrontab();
} catch (\Exception $e) {
$this->get('logger')->error('Cron sync failed: '.$e->getMessage());
}
$mockCronManager = $this->createMock(\DZ\CronManagerBundle\Manager\CronManager::class);
$mockCronManager->method('getOutput')->willReturn(['job1', 'job2']);
$this->container->set('dz_cron.manager', $mockCronManager);
Permission Issues
writeToCrontab() requires write access to /etc/crontab or the user’s crontab file.chmod on the crontab file or use sudo in the command (not recommended for production).anacron for execution.Schedule Parsing Quirks
@daily, @hourly).cron-expression to validate:
use Cron\CronExpression;
if (!CronExpression::isValid('*/5 * * * *')) {
throw new \InvalidArgumentException('Invalid cron schedule');
}
Race Conditions
writeToCrontab() can corrupt the crontab.LockFactory:
$lock = $this->get('lock.factory')->createLock('cron_sync_lock', 60);
if ($lock->acquire()) {
$cronManager->writeToCrontab();
$lock->release();
}
Deprecated Symfony2 Syntax
spatie/cron-expression.Inspect Output
Use getOutput() to debug parsed jobs:
$jobs = $cronManager->getOutput();
print_r($jobs); // Array of parsed cron entries
Dry Runs
Test changes locally by writing to a temp file instead of /etc/crontab:
# config/packages/dz_cron_manager.yaml
dz_cron_manager:
crontab_file: "%kernel.project_dir%/var/cron_test"
Logging Enable debug mode to log cron operations:
$cronManager->setDebug(true);
Custom Job Entities
Extend \DZ\CronManagerBundle\Entity\CronJob to add metadata:
class ExtendedCronJob extends CronJob
{
private $priority;
// Add getters/setters and use in validation.
}
Pre/Post-Sync Hooks
Subscribe to events (if the bundle supports them) or wrap writeToCrontab():
$cronManager->writeToCrontab();
$this->dispatchEvent('cron.sync.post');
Alternative Storage Store jobs in the database instead of the crontab:
$jobs = $this->getDoctrine()->getRepository(CronJob::class)->findAll();
foreach ($jobs as $job) {
$cronManager->addJob($job);
}
$cronManager->writeToCrontab();
UI Integration Use Twig to render a cron dashboard:
{% for job in cronManager.getOutput() %}
<li>{{ job.command }} ({{ job.schedule }})</li>
{% endfor %}
How can I help you explore Laravel packages today?