2017 © Pedro Peláez
 

yii2-extension crontab

Crontab support extension for the Yii2 framework

image

yii2tech/crontab

Crontab support extension for the Yii2 framework

  • Friday, November 3, 2017
  • by klimov-paul
  • Repository
  • 25 Watchers
  • 135 Stars
  • 20,306 Installations
  • PHP
  • 1 Dependents
  • 1 Suggesters
  • 28 Forks
  • 0 Open issues
  • 5 Versions
  • 7 % Grown

The README.md

, (*1)

Crontab Extension for Yii 2


This extension adds Crontab setup support., (*2)

For license information check the LICENSE-file., (*3)

Latest Stable Version Total Downloads Build Status, (*4)

Requirements

This extension requires Linux OS. 'crontab' should be installed and cron daemon should be running., (*5)

Installation

The preferred way to install this extension is through composer., (*6)

Either run, (*7)

php composer.phar require --prefer-dist yii2tech/crontab

or add, (*8)

"yii2tech/crontab": "*"

to the require section of your composer.json., (*9)

Usage

You can setup cron tab using [[yii2tech\crontab\CronTab]], for example:, (*10)

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:, (*11)

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., (*12)

Parsing cron jobs

[[yii2tech\crontab\CronJob]] composes a cron job line, like:, (*13)

0 0 * * * php /path/to/my/project/yii some-cron

However it can also parse such lines filling up own internal attributes. For example:, (*14)

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'

Merging cron jobs

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:, (*15)

0 0 * * * php /path/to/my/project/yii daily-cron

running following code:, (*16)

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:, (*17)

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:, (*18)

0 0 * * * php /path/to/my/project/yii some-cron

running following code:, (*19)

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:, (*20)

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: bool function (string $line) - if function returns true the line should be removed. For example, if current crontab is following:, (*21)

0 0 * * * php /path/to/my/project/yii some-cron

running following code:, (*22)

use yii2tech\crontab\CronTab;

$cronTab = new CronTab();
$cronTab->mergeFilter = '/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:, (*23)

15 2 * * * php /path/to/my/project/yii some-cron

Extra lines setup

Crontab file may content additional lines beside jobs specifications. It may contain comments or extra shell configuration. For example:, (*24)

# this crontab created by my application
SHELL=/bin/sh
PATH=/usr/bin:/usr/sbin

0 0 * * * php /path/to/my/project/yii some-cron

You may append such extra lines into the crontab using [[yii2tech\crontab\CronTab::$headLines]]. For example:, (*25)

use yii2tech\crontab\CronTab;

$cronTab = new CronTab();
$cronTab->headLines = [
    '# this crontab created by my application',
    'SHELL=/bin/sh',
    'PATH=/usr/bin:/usr/sbin',
];
$cronTab->setJobs([
    [
        'min' => '0',
        'hour' => '0',
        'command' => 'php /path/to/project/yii some-cron',
    ],
]);
$cronTab->apply();

Note: usage of the headLines may produce unexpected results, while merging crontab with existing one., (*26)

User setup

Each Linux system user has his own crontab. Ownership of crontab affected by this extension is determined by the user running the PHP script. For the web application it is usually 'apache', for the console application - current local user or root. Thus crontab application from web application and from console application will produce 2 separated cron jobs list for 2 different system users., (*27)

You may explicitly setup name of the user whose crontab is to be affected via [[yii2tech\crontab\CronTab::$username]]. For example:, (*28)

use yii2tech\crontab\CronTab;

$cronTab = new CronTab();
$cronTab->username = 'www-data'; // apply crontab for 'www-data' user
$cronTab->setJobs([
    [
        'min' => '0',
        'hour' => '0',
        'command' => 'php /path/to/project/yii some-cron',
    ],
]);
$cronTab->apply();

However, this will work only in case PHP script is running from privileged user (e.g. 'root')., (*29)

The Versions

03/11 2017

dev-master

9999999-dev

Crontab support extension for the Yii2 framework

  Sources   Download

BSD-3-Clause

The Requires

 

by Paul Klimov

yii2 cronjob cron crontab

03/11 2017

1.0.3

1.0.3.0

Crontab support extension for the Yii2 framework

  Sources   Download

BSD-3-Clause

The Requires

 

by Paul Klimov

yii2 cronjob cron crontab

25/04 2016

1.0.2

1.0.2.0

Crontab support extension for the Yii2 framework

  Sources   Download

BSD-3-Clause

The Requires

 

by Paul Klimov

yii2 cronjob cron crontab

10/02 2016

1.0.1

1.0.1.0

Crontab support extension for the Yii2 framework

  Sources   Download

BSD-3-Clause

The Requires

 

by Paul Klimov

yii2 cronjob cron crontab

26/12 2015

1.0.0

1.0.0.0

Crontab support extension for the Yii2 framework

  Sources   Download

BSD-3-Clause

The Requires

 

by Paul Klimov

yii2 cronjob cron crontab