agentsib/crontab-bundle
AgentSIB Crontab Bundle for PHP/Laravel: a work-in-progress package intended to help manage crontab entries from your application. Currently in early development; features and API may change and documentation is minimal.
Installation:
composer require agentsib/crontab-bundle
Add to AppKernel.php (Symfony2) or bundles.php (Symfony3):
new AgentSIB\CrontabBundle\AgentSIBCrontabBundle(),
Configuration:
Enable the bundle in config.yml:
agentsib_crontab:
commands: # List of console commands to schedule
- app:command1
- app:command2
cron_file: '%kernel.root_dir%/../crontab' # Path to crontab file
user: 'www-data' # User to run cron jobs as
First Use Case: Schedule a command to run daily at midnight:
agentsib_crontab:
commands:
- { command: 'app:your_command', schedule: '0 0 * * *' }
Run the agentsib:crontab:generate command to write the crontab file:
php app/console agentsib:crontab:generate
Defining Jobs:
agentsib_crontab:
commands:
- { command: 'app:backup', schedule: '0 2 * * 0' } # Weekly Sunday at 2 AM
- { command: 'app:cleanup', schedule: '0 3 * * *' } # Daily at 3 AM
Dynamic Scheduling:
parameters:
cron_backup_time: '0 4 * * *' # Override via env: BACKUP_TIME
agentsib_crontab:
commands:
- { command: 'app:backup', schedule: '%backup_time%' }
Integration with Console Commands:
class BackupCommand extends ContainerAwareCommand {
protected function execute(InputInterface $input, OutputInterface $output) {
if ($this->getContainer()->getParameter('is_cron')) {
// Cron-specific logic (e.g., silent mode)
$output->setVerbosity(OutputInterface::VERBOSITY_QUIET);
}
// ... rest of the command
}
}
Generating and Managing Crontab:
php app/console agentsib:crontab:generate
service cron reload # Linux
php app/console agentsib:crontab:remove
Outdated Package:
mtdowling/cron-expression).Crontab File Permissions:
%kernel.root_dir%/../crontab) is writable by the cron user (e.g., www-data).sudo chown www-data:www-data %kernel.root_dir%/../crontab
sudo chmod 644 %kernel.root_dir%/../crontab
Command Paths:
app:command) to avoid ambiguity.Cron Syntax Errors:
* in the wrong field).0 0 * * is invalid; use 0 0 * * *).Logging and Debugging:
grep CRON /var/log/syslog
agentsib_crontab:
log_commands: true
Environment-Specific Schedules:
dev/prod:
# config_dev.yml
agentsib_crontab:
commands:
- { command: 'app:dev-task', schedule: '*/5 * * * *' } # Every 5 mins in dev
Conditional Execution:
if (!$this->getContainer()->getParameter('enable_cron_job')) {
return 0;
}
Backup Old Crontab:
/etc/crontab or the user’s crontab (crontab -l) before generating:
sudo cp /etc/crontab /etc/crontab.bak
Testing Locally:
symfony/process to simulate cron jobs:
$process = new Process(['php', 'app/console', 'app:your_command']);
$process->run();
Extension Points:
CrontabGenerator service to customize crontab generation logic:
services:
agentsib_crontab.generator:
class: Your\Bundle\Crontab\CustomGenerator
arguments: ['@service_container']
tags:
- { name: agentsib_crontab.generator }
How can I help you explore Laravel packages today?