PHP Класс yii2tech\crontab\CronTab

Note: Each system user has his own crontab, make sure you always run this class for the same user. For the web application it is usually 'apache', for the console application - current local user or root. Example usage: php 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();
См. также: CronJob
См. также: http://en.wikipedia.org/wiki/Crontab
С версии: 1.0
Автор: Paul Klimov ([email protected])
Наследование: extends yii\base\Component
Показать файл Открыть проект Примеры использования класса

Открытые свойства

Свойство Тип Описание
$binPath path to the 'crontab' command, for example: '/usr/bin/crontab'. Default is 'crontab' assuming 'crontab' command is available in OS shell.
$defaultJobConfig default configuration for the cron job objects.
$headLines list of lines, which should be added at the beginning of the crontab. You can put comment lines and shell configuration here. For example: php [ '# crontab created by my application', 'SHELL=/bin/sh', 'PATH=/usr/bin:/usr/sbin', ]
$mergeFilter filter, which indicates whether existing cron job should be removed on cron tab merging. Value could be a plain string, which presence in the cron job line indicates it should be removed, or a callable of following signature: boolean function (string $line), which should return true, if line should be removed.

Открытые методы

Метод Описание
apply ( ) : static Applies the current {@link jobs} to the current user crontab.
applyFile ( string $filename ) : static Setup the cron jobs from given file.
getCurrentLines ( ) : array Returns current cron jobs setup in the system for current user.
getJobs ( ) : array
getLines ( ) : array Returns the crontab lines composed from [[jobs]].
remove ( ) : static Removes current [[jobs]] from the current user crontab.
removeAll ( ) : static Removes all cron jobs for the current user.
saveToFile ( string $fileName ) : integer Saves the current jobs into the text file.
setJobs ( array $jobs ) : static

Защищенные методы

Метод Описание
applyLines ( array $lines ) Applies given lines to current user crontab.
composeFileContent ( array $lines ) : string Composes the crontab file content from given lines.
composeJobLine ( yii2tech\crontab\CronJob | array $job ) : string Composes cron job line from configuration.
createJob ( array $config ) : yii2tech\crontab\CronJob Creates cron job instance from its array configuration.
mergeLines ( array $currentLines, array $newLines ) : array Merges existing crontab lines with new ones, applying [[mergeFilter]].

Описание методов

apply() публичный Метод

This method will merge new jobs with the ones already set in the system.
public apply ( ) : static
Результат static self reference.

applyFile() публичный Метод

Setup the cron jobs from given file.
public applyFile ( string $filename ) : static
$filename string file name.
Результат static self reference.

applyLines() защищенный Метод

Applies given lines to current user crontab.
protected applyLines ( array $lines )
$lines array crontab lines.

composeFileContent() защищенный Метод

Composes the crontab file content from given lines.
protected composeFileContent ( array $lines ) : string
$lines array crontab lines.
Результат string crontab file content.

composeJobLine() защищенный Метод

Composes cron job line from configuration.
protected composeJobLine ( yii2tech\crontab\CronJob | array $job ) : string
$job yii2tech\crontab\CronJob | array cron job line or configuration.
Результат string cron job line.

createJob() защищенный Метод

Creates cron job instance from its array configuration.
protected createJob ( array $config ) : yii2tech\crontab\CronJob
$config array cron job configuration.
Результат yii2tech\crontab\CronJob cron job instance.

getCurrentLines() публичный Метод

Returns current cron jobs setup in the system for current user.
public getCurrentLines ( ) : array
Результат array cron job lines.

getJobs() публичный Метод

public getJobs ( ) : array
Результат array

getLines() публичный Метод

Returns the crontab lines composed from [[jobs]].
public getLines ( ) : array
Результат array crontab lines.

mergeLines() защищенный Метод

Merges existing crontab lines with new ones, applying [[mergeFilter]].
protected mergeLines ( array $currentLines, array $newLines ) : array
$currentLines array lines to be merged to
$newLines array lines to be merged from.
Результат array merged lines

remove() публичный Метод

Removes current [[jobs]] from the current user crontab.
public remove ( ) : static
Результат static self reference.

removeAll() публичный Метод

Removes all cron jobs for the current user.
public removeAll ( ) : static
Результат static self reference.

saveToFile() публичный Метод

Saves the current jobs into the text file.
public saveToFile ( string $fileName ) : integer
$fileName string output file name.
Результат integer number of written bytes.

setJobs() публичный Метод

public setJobs ( array $jobs ) : static
$jobs array
Результат static self reference

Описание свойств

$binPath публичное свойство

path to the 'crontab' command, for example: '/usr/bin/crontab'. Default is 'crontab' assuming 'crontab' command is available in OS shell.
public $binPath

$defaultJobConfig публичное свойство

default configuration for the cron job objects.
public $defaultJobConfig

$headLines публичное свойство

list of lines, which should be added at the beginning of the crontab. You can put comment lines and shell configuration here. For example: php [ '# crontab created by my application', 'SHELL=/bin/sh', 'PATH=/usr/bin:/usr/sbin', ]
public $headLines

$mergeFilter публичное свойство

filter, which indicates whether existing cron job should be removed on cron tab merging. Value could be a plain string, which presence in the cron job line indicates it should be removed, or a callable of following signature: boolean function (string $line), which should return true, if line should be removed.
public $mergeFilter