This extension adds Crontab setup support.
For license information check the LICENSE-file.
This extension requires Linux OS. 'crontab' should be installed and cron daemon should be running.
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist yii2tech/crontab
or add
"yii2tech/crontab": "*"
to the require section of your composer.json.
You can setup cron tab using [[yii2tech\crontab\CronTab]], for example:
use yii2tech\crontab\CronTab;
$cronTab = new CronTab();
$cronTab->setJobs([
[
'min' => '0',
'hour' => '0',
'command' => 'php /path/to/project/yii some-cron',
],
[
'line' => '0 0 * * * php /path/to/project/yii another-cron'
]
]);
$cronTab->apply();
You may specify particular cron job using [[yii2tech\crontab\CronJob]] instance, for example:
use yii2tech\crontab\CronJob;
use yii2tech\crontab\CronTab;
$cronJob = new CronJob();
$cronJob->min = '0';
$cronJob->hour = '0';
$cronJob->command = 'php /path/to/project/yii some-cron';
$cronTab = new CronTab();
$cronTab->setJobs([
$cronJob
]);
$cronTab->apply();
Tip: [[yii2tech\crontab\CronJob]] is a descendant of [[yii\base\Model]] and have built in validation rules for each parameter, thus it can be used in the web forms to create a cron setup interface.
[[yii2tech\crontab\CronJob]] composes a cron job line, like:
0 0 * * * php /path/to/my/project/yii some-cron
However it can also parse such lines filling up own internal attributes. For example:
use yii2tech\crontab\CronJob;
$cronJob = new CronJob();
$cronJob->setLine('0 0 * * * php /path/to/my/project/yii some-cron');
echo $cronJob->min; // outputs: '0'
echo $cronJob->hour; // outputs: '0'
echo $cronJob->day; // outputs: '*'
echo $cronJob->month; // outputs: '*'
echo $cronJob->command; // outputs: 'php /path/to/my/project/yii some-cron'
Method [[yii2tech\crontab\CronTab::apply()]] adds all specified cron jobs to crontab, keeping already exiting cron jobs intact. For example, if current crontab is following:
0 0 * * * php /path/to/my/project/yii daily-cron
running following code:
use yii2tech\crontab\CronTab;
$cronTab = new CronTab();
$cronTab->setJobs([
[
'min' => '0',
'hour' => '0',
'weekDay' => '5',
'command' => 'php /path/to/project/yii weekly-cron',
],
]);
$cronTab->apply();
will produce following crontab:
0 0 * * * php /path/to/my/project/yii daily-cron
0 0 * * 5 php /path/to/my/project/yii weekly-cron
While merging crontab lines [[yii2tech\crontab\CronTab::apply()]] avoids duplication, so same cron job will never be added twice. However while doing this, lines are compared by exact match, inlcuding command and time pattern. If same command added twice with different time pattern - 2 crontab records will be present. For example, if current crontab is following:
0 0 * * * php /path/to/my/project/yii some-cron
running following code:
use yii2tech\crontab\CronTab;
$cronTab = new CronTab();
$cronTab->setJobs([
[
'min' => '15',
'hour' => '2',
'command' => 'php /path/to/project/yii some-cron',
],
]);
$cronTab->apply();
will produce following crontab:
0 0 * * * php /path/to/my/project/yii some-cron
15 2 * * * php /path/to/my/project/yii some-cron
You may interfere in merging process using [[yii2tech\crontab\CronTab::mergeFilter]], which allows indicating
those existing cron jobs, which should be removed while merging. Its value could be a plain string - in this case
all lines, which contains this string as a substring will be removed, or a PHP callable of the following signature:
boolean function (string $line)
- if function returns true
the line should be removed.
For example, if current crontab is following:
0 0 * * * php /path/to/my/project/yii some-cron
running following code:
use yii2tech\crontab\CronTab;
$cronTab = new CronTab();
$cronTab->filter = '/path/to/project/yii'; // filter all invocation of Yii console
$cronTab->setJobs([
[
'min' => '15',
'hour' => '2',
'command' => 'php /path/to/project/yii some-cron',
],
]);
$cronTab->apply();
will produce following crontab:
15 2 * * * php /path/to/my/project/yii some-cron