taueres/easy-crontab

Object oriented API for reading and writing crontab

dev-master 2015-09-27 16:13 UTC

This package is not auto-updated.

Last update: 2025-09-14 00:37:06 UTC


README

Object oriented API for reading and writing to/from crontab.

Build Status

Installing

Via Composer

composer require taueres/easy-crontab

Otherwise manually

Clone git repository.

git clone https://github.com/taueres/easy-crontab.git ./vendor/easy-crontab

Add the following PSR-4 rules to your autoload system.

"psr-4": {
    "EasyCrontab\\": "vendor/easy-crontab/src/EasyCrontab",
    "EasyCrontab\\Test\\": "vendor/easy-crontab/test"
}

Install EasyCrontab dependencies.

"require": {
    "symfony/process": "^2.7",
    "symfony/dependency-injection": "^2.7",
    "symfony/config": "^2.7"
}

Examples

EasyCrontab is very easy to grasp.

The following examples will cover common use cases of EasyCrontab.

Print command of the first job registered

$crontab = EasyCrontab\CrontabFactory::getCrontab();
$jobs = $crontab->getJobs();
echo $jobs[0]->getCommand();

Add a new job to crontab

$job = new EasyCrontab\Job();
$job->setMinute('*/5');
$job->setHour('5');
$job->setDayOfMonth('*');
$job->setMonth('*');
$job->setDayOfWeek('*');
$job->setCommand('php --version');

$crontab = EasyCrontab\CrontabFactory::getCrontab();
$crontab->addJob($job);
$crontab->save();

Edit job info

$crontab = EasyCrontab\CrontabFactory::getCrontab();
$jobs = $crontab->getJobs();
$jobs[0]->setDayOfWeek('3');
$crontab->save();

Remove job from crontab

$crontab = EasyCrontab\CrontabFactory::getCrontab();
$jobs = $crontab->getJobs();
$crontab->removeJob($jobs[0]);
$crontab->save();