PHP Class 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();
See also: CronJob
See also: http://en.wikipedia.org/wiki/Crontab
Since: 1.0
Author: Paul Klimov ([email protected])
Inheritance: extends yii\base\Component
显示文件 Open project: yii2tech/crontab Class Usage Examples

Public Properties

Property Type Description
$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.

Public Methods

Method Description
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

Protected Methods

Method Description
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]].

Method Details

apply() public method

This method will merge new jobs with the ones already set in the system.
public apply ( ) : static
return static self reference.

applyFile() public method

Setup the cron jobs from given file.
public applyFile ( string $filename ) : static
$filename string file name.
return static self reference.

applyLines() protected method

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

composeFileContent() protected method

Composes the crontab file content from given lines.
protected composeFileContent ( array $lines ) : string
$lines array crontab lines.
return string crontab file content.

composeJobLine() protected method

Composes cron job line from configuration.
protected composeJobLine ( yii2tech\crontab\CronJob | array $job ) : string
$job yii2tech\crontab\CronJob | array cron job line or configuration.
return string cron job line.

createJob() protected method

Creates cron job instance from its array configuration.
protected createJob ( array $config ) : yii2tech\crontab\CronJob
$config array cron job configuration.
return yii2tech\crontab\CronJob cron job instance.

getCurrentLines() public method

Returns current cron jobs setup in the system for current user.
public getCurrentLines ( ) : array
return array cron job lines.

getJobs() public method

public getJobs ( ) : array
return array

getLines() public method

Returns the crontab lines composed from [[jobs]].
public getLines ( ) : array
return array crontab lines.

mergeLines() protected method

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.
return array merged lines

remove() public method

Removes current [[jobs]] from the current user crontab.
public remove ( ) : static
return static self reference.

removeAll() public method

Removes all cron jobs for the current user.
public removeAll ( ) : static
return static self reference.

saveToFile() public method

Saves the current jobs into the text file.
public saveToFile ( string $fileName ) : integer
$fileName string output file name.
return integer number of written bytes.

setJobs() public method

public setJobs ( array $jobs ) : static
$jobs array
return static self reference

Property Details

$binPath public_oe property

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

$defaultJobConfig public_oe property

default configuration for the cron job objects.
public $defaultJobConfig

$headLines public_oe property

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 public_oe property

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