Installation:
composer require ecentria/crontab-bundle
Register the bundle in config/bundles.php (Symfony 4+) or AppKernel.php (Symfony 3-):
Ecentria\Bundle\CrontabBundle\EcentriaCrontabBundle::class => ['all' => true],
Basic Configuration:
Create config/packages/ecentria_crontab.yaml (Symfony 4+) or app/config/config.yml (Symfony 3-):
ecentria_crontab:
path: '/etc/crontab' # Default path; adjust if needed
jobs:
- name: 'my_job'
command: 'php /path/to/artisan my:command'
schedule: '*/5 * * * *' # Every 5 minutes
First Use Case: Run the command to generate/update crontab entries:
php bin/console ecentria:crontab:setup
Verify entries in /etc/crontab (or your configured path).
Development Workflow:
ecentria:crontab:setup --dry-run to preview changes without modifying the crontab.~/.test_crontab) by setting path in config.Deployment Workflow:
config/packages/ecentria_crontab.yaml).ecentria:crontab:setup post-deploy:
php bin/console ecentria:crontab:setup --force
chmod 600 /etc/crontab) for security.Dynamic Job Management:
// src/Command/AddCronJobCommand.php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Ecentria\Bundle\CrontabBundle\Service\CrontabManager;
class AddCronJobCommand extends Command {
protected static $defaultName = 'app:crontab:add';
private $crontabManager;
public function __construct(CrontabManager $crontabManager) {
$this->crontabManager = $crontabManager;
}
protected function execute(InputInterface $input, OutputInterface $output) {
$this->crontabManager->addJob([
'name' => $input->getArgument('name'),
'command' => $input->getArgument('command'),
'schedule' => $input->getArgument('schedule'),
]);
return Command::SUCCESS;
}
}
Environment-Specific Configs:
config/packages/ecentria_crontab_{env}.yaml) to define jobs per environment (dev/staging/prod).php /path/to/script.sh >> /var/log/cron.log 2>&1).jobs:
- name: 'send_queued_emails'
command: 'php artisan queue:work --daemon --sleep=3 --tries=1'
schedule: '* * * * *'
command: 'php {{ bin_path }} artisan my:command'), and resolve them in a custom command.setup:
cp /etc/crontab /etc/crontab.bak && php bin/console ecentria:crontab:setup
Permissions:
www-data) has execute permissions for scheduled commands.sudo in commands or adjust file permissions:
command: 'sudo -u www-data php /path/to/artisan my:command'
Path Resolution:
command are required. Relative paths may fail in cron’s environment.realpath() or environment variables to resolve paths dynamically.Environment Variables:
.env are not loaded by default.command: '. /path/to/.env && php /path/to/artisan my:command'
Overwriting Existing Entries:
--append to merge changes (if supported) or manually merge configs.Time Zone Issues:
TZ=UTC in commands.Dry Runs:
--dry-run before applying changes to production:
php bin/console ecentria:crontab:setup --dry-run
Check Output:
command: 'php /path/to/artisan my:command >> /var/log/my_job.log 2>&1'
Verify Syntax:
crontab -l to manually check the crontab syntax after updates.Logs:
// config/packages/monolog.yaml
handlers:
cron:
type: stream
path: '%kernel.logs_dir%/cron.log'
level: debug
Custom Matchers:
CrontabMatcher service:
// src/Service/CustomCrontabMatcher.php
namespace App\Service;
use Ecentria\Bundle\CrontabBundle\Matcher\CrontabMatcherInterface;
class CustomCrontabMatcher implements CrontabMatcherInterface {
public function match(string $line, array $config): bool {
// Custom logic
}
}
services.yaml:
services:
App\Service\CustomCrontabMatcher:
tags: ['ecentria.crontab.matcher']
Pre/Post Setup Hooks:
// src/EventListener/CrontabSetupListener.php
namespace App\EventListener;
use Ecentria\Bundle\CrontabBundle\Event\CrontabSetupEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CrontabSetupListener implements EventSubscriberInterface {
public static function getSubscribedEvents() {
return [
CrontabSetupEvent::PRE_SETUP => 'onPreSetup',
CrontabSetupEvent::POST_SETUP => 'onPostSetup',
];
}
public function onPreSetup(CrontabSetupEvent $event) {
// Logic before setup
}
public function onPostSetup(CrontabSetupEvent $event) {
// Logic after setup
}
}
Dynamic Config Loading:
CrontabConfigProvider:
// src/Service/DynamicCrontabConfigProvider.php
namespace App\Service;
use Ecentria\Bundle\CrontabBundle\Provider\CrontabConfigProviderInterface;
class DynamicCrontabConfigProvider implements CrontabConfigProviderInterface {
public function getConfig(): array {
return $this->fetchFromDatabase(); // Custom logic
}
}
services.yaml:
services:
App\Service\DynamicCrontabConfigProvider:
tags: ['ecentria.crontab.config_provider']
Idempotency:
Testing:
php bin/console directly before deploying to production.Documentation:
jobs:
- name: 'backup_database'
command: 'php artisan
How can I help you explore Laravel packages today?