• This repository has been archived on 10/Jan/2022
  • Stars
    star
    184
  • Rank 209,187 (Top 5 %)
  • Language
    PHP
  • License
    Other
  • Created over 9 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Yii2 extension for crontab support

Crontab Extension for Yii 2


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 [[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.

Parsing cron jobs

[[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'

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:

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: 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 [[yii2tech\crontab\CronTab::$headLines]]. 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 [[yii2tech\crontab\CronTab::$username]]. 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').

More Repositories

1

ar-softdelete

Soft delete behavior for ActiveRecord
PHP
202
star
2

balance

Balance accounting (bookkeeping) system based on debit and credit principle
PHP
174
star
3

file-storage

This package has been abandoned. See: https://www.patreon.com/posts/59332556
PHP
119
star
4

ar-position

ActiveRecord behavior, which provides ability for custom records order setup
PHP
113
star
5

admin

Admin pack (actions, widgets, etc) for Yii2
PHP
102
star
6

selfupdate

Basic script for project self update from VCS
PHP
92
star
7

csv-grid

Yii2 extension for CSV export
PHP
89
star
8

ar-linkmany

ActiveRecord behavior for saving many-to-many relations
PHP
87
star
9

illuminate

Yii2 to Laravel Migration Package
PHP
87
star
10

embedded

Support embedded models usage for complex ActiveRecord like MongoDB or ElasticSearch
PHP
81
star
11

spreadsheet

Yii2 extension for export to Excel
PHP
81
star
12

html2pdf

Yii2 component for HTML to PDF conversion
PHP
75
star
13

filedb

ActiveRecord for static data definitions based on files
PHP
73
star
14

config

Yii2 application runtime configuration support
PHP
62
star
15

sitemap

Site map creation support
PHP
59
star
16

project-template

Yii2 Project Template
PHP
54
star
17

content

Content management system for Yii2
PHP
53
star
18

ar-variation

Variation behavior for ActiveRecord
PHP
49
star
19

ar-dynattribute

Provide ActiveRecord dynamic attributes stored into the single field in serialized state
PHP
44
star
20

ar-file

This package has been abandoned. See: https://www.patreon.com/posts/59332556
PHP
36
star
21

ar-role

ActiveRecord behavior, which provides relation roles (table inheritance)
PHP
36
star
22

authlog

Identity auth tracking
PHP
33
star
23

ar-search

Provides unified search model for Yii ActiveRecord
PHP
31
star
24

ar-eagerjoin

ActiveRecord behavior, which provides relation eager loading by join without extra query
PHP
21
star
25

behavior-trait

Allows handling events via inline declared methods, which can be added by traits
PHP
20
star
26

install

basic script for project installation
PHP
18
star
27

activemail

Provides ActiveMessage mailing approach for Yii2
PHP
17
star
28

https

Secure connection (https) handling
PHP
11
star
29

model-change

Yii2 model data and state change tracking
PHP
11
star