crontab

Yii2 extension for crontab support

Github星跟蹤圖

This extension adds Crontab setup support.

For license information check the LICENSE-file.

Latest Stable Version
Total Downloads
Build Status

Requirements

This extension requires Linux OS. 'crontab' should be installed and cron daemon should be running.

Installation

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.

Usage

You can setup cron tab using , 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 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: is a descendant of and have built in validation rules for each
parameter, thus it can be used in the web forms to create a cron setup interface.

Parsing cron jobs

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'

Merging cron jobs

Method 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 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 , 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:

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

running following code:

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:

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:

# 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 . For example:

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.

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.

You may explicitly setup name of the user whose crontab is to be affected via .
For example:

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').

主要指標

概覽
名稱與所有者yii2tech/crontab
主編程語言PHP
編程語言PHP (語言數: 1)
平台
許可證Other
所有者活动
創建於2015-06-18 12:28:48
推送於2021-11-05 11:05:23
最后一次提交2019-07-03 12:14:09
發布數5
最新版本名稱1.0.4 (發布於 )
第一版名稱1.0.0 (發布於 )
用户参与
星數184
關注者數21
派生數52
提交數33
已啟用問題?
問題數11
打開的問題數0
拉請求數1
打開的拉請求數0
關閉的拉請求數7
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?