EasyCrontab
Object oriented API for reading and writing to/from crontab., (*1)
, (*2)
Installing
Via Composer
composer require taueres/easy-crontab
Otherwise manually
Clone git repository., (*3)
git clone https://github.com/taueres/easy-crontab.git ./vendor/easy-crontab
Add the following PSR-4 rules to your autoload system., (*4)
"psr-4": {
"EasyCrontab\\": "vendor/easy-crontab/src/EasyCrontab",
"EasyCrontab\\Test\\": "vendor/easy-crontab/test"
}
Install EasyCrontab dependencies., (*5)
"require": {
"symfony/process": "^2.7",
"symfony/dependency-injection": "^2.7",
"symfony/config": "^2.7"
}
Examples
EasyCrontab is very easy to grasp., (*6)
The following examples will cover common use cases of EasyCrontab., (*7)
Print command of the first job registered
$crontab = EasyCrontab\CrontabFactory::getCrontab();
$jobs = $crontab->getJobs();
echo $jobs[0]->getCommand();
Add a new job to crontab
$job = new EasyCrontab\Job();
$job->setMinute('*/5');
$job->setHour('5');
$job->setDayOfMonth('*');
$job->setMonth('*');
$job->setDayOfWeek('*');
$job->setCommand('php --version');
$crontab = EasyCrontab\CrontabFactory::getCrontab();
$crontab->addJob($job);
$crontab->save();
Edit job info
$crontab = EasyCrontab\CrontabFactory::getCrontab();
$jobs = $crontab->getJobs();
$jobs[0]->setDayOfWeek('3');
$crontab->save();
Remove job from crontab
$crontab = EasyCrontab\CrontabFactory::getCrontab();
$jobs = $crontab->getJobs();
$crontab->removeJob($jobs[0]);
$crontab->save();